[Rcpp-devel] Rcpp Lists and memory corruption

Ilya E bbc5019 at live.com
Sat Mar 31 09:04:44 CEST 2012

Are there any known issues regarding memory corruption caused by lists created inside of Rcpp? I started noticing some inconsistencies in the data returned by my app, and was able to reduce the issue to the following reproducible example. It seems to suggest that lists created in Rcpp and returned to R can cause sporadic memory corruption. See code below for a demonstration:

testFun <- local( {
    fun <- cxxfunction(        signature(), '		return Rcpp::List::create(Rcpp::Named( "testVar" ) = wrap(10));        ', plugin = "Rcpp" )
    function(){        fun()    }} )
# generate the result set -- you would expect a numeric vector that consists of repetition of the value 10testOutcome = mapply(	function(i) {		fTouch = testFun()		return(fTouch$testVar)	},	1:5612062)
# The result ends up being something other than what you'd expecttypeof(testOutcome)	for (i in 1:length(testOutcome)) {	if (testOutcome[[i]] != 10) {		cat(i, "\n")	}}

You'll notice two things here. First, mapply fails to coerce the returned results to a vector (since in some cases the return from testFun seems to have more than 1 element) and second, not all return values are equal to 10, as you would expect. The actual position in the vector of the corrupted data is sporadic. If you keep running this over and over again, you'll see the wrong value in different places. Sometimes you can even get R to crash after multiple runs. On rare occasions there will be no errors at all. For some reason you tend to see the most errors on the first run, especially if you open a new instance of R.
If you replace the line "fTouch = testFun()" with a native call to R "fTouch = list(testVar = 10)" then everything works fine. Also returning a NumericVector instead of a list also works fine -- the issue seems to appear after encapsulating the return value in a list inside of Rcpp.
I am using R 2.14.2 on Win32 & Win64. Rcpp version 0.9.10.


More information about the Rcpp-devel mailing list