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

Hervé Perdry herve.perdry at protonmail.com
Wed Apr 29 18:50:00 CEST 2026


Hello,

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.

Yours

HP
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20260429/731b33f9/attachment.htm>


More information about the Rcpp-devel mailing list