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

Dirk Eddelbuettel edd at debian.org
Wed Apr 29 19:25:38 CEST 2026


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


More information about the Rcpp-devel mailing list