[Rcpp-devel] Memory leak when a warning is catched before the end of a function

Kevin Ushey kevinushey at gmail.com
Wed Apr 29 19:31:33 CEST 2026


Thanks; I think the issue here is real.

The problem here is that the tryCatch handler is causing the
Rf_warning call to longjmp out of the C++ frame, and there's a
std::string being lost after the jump.

I'll promote this to the Rcpp issue tracker and prepare a patch.

Best,
Kevin

On Wed, Apr 29, 2026 at 10:26 AM Dirk Eddelbuettel <edd at debian.org> wrote:
>
>
> Hi Herve,
>
> On 29 April 2026 at 16:50, Hervé Perdry wrote:
> | I'm sorry if this is not the right place to post this, but I ran across this phenomenon and I think some people here might find it of interest.
> | Here is a test that runs well (it sends a warning as we're trying to push back to vectors of different sizes in a data frame):
> |
> | R -d "valgrind --tool=memcheck --leak-check=full --track-origins=yes" \
> |       -e "Rcpp::cppFunction('Rcpp::DataFrame test(SEXP a, SEXP b) { \
> |       Rcpp::DataFrame DF; \
> |       DF.push_back(a);    \
> |       DF.push_back(b);    \
> |       return DF; }');     \
> |             test(1:4, 11:15)"
> |
> | And here is a test where valgrind reports that 78 bytes are "definitely lost":
> |
> | R -d "valgrind --tool=memcheck --leak-check=full --track-origins=yes" \
> |       -e "Rcpp::cppFunction('Rcpp::DataFrame test(SEXP a, SEXP b) { \
> |       Rcpp::DataFrame DF; \
> |       DF.push_back(a);    \
> |       DF.push_back(b);    \
> |       return DF; }');     \
> |             tryCatch( test(1:4, 11:15), warning = function(w) {} )"
> |
> | My understanding is that when run in "tryCatch" as above, the test function is aborted and the object DF is not properly freed.
>
> Well what is your real question here? In R you cannot have a data.frame with
> different column lengths, and Rcpp long documented that it does not recycle.
>
> C++ still has resource control so the destructor should still be reached. You
> can turn on debug mode / add a 'print' to the destructor.
>
> Dirk
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


More information about the Rcpp-devel mailing list