Thanks for this. I can reproduce the issue with the devel version. I get :

> Filter( function(x) x != 10, testOutcome )
[[1]]
[1] "testVar"

[[2]]
[1] "testVar"

[[3]]
[1] "testVar"

[[4]]
[1] "testVar"

[[5]]
[1] "testVar"

[[6]]
[1] "testVar"

[[7]]
[1] "testVar"

[[8]]
[1] "testVar"

[[9]]
[1] "testVar"

[[10]]
[1] "testVar"

[[11]]
[1] "testVar"

[[12]]
[1] "testVar"

[[13]]
[1] "testVar"

[[14]]
[1] "testVar"


Not sure what is going on.

Romain

Le 31/03/12 09:20, Ilya E a écrit :
(Apologies for sending this twice -- the formatting on the first email got 
completely mangled)


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:

require(inline)
require(Rcpp)

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 10
testOutcome = mapply(
function(i) {
fTouch = testFun()
return(fTouch$testVar)
},
1:5612062)

# The result ensd up being something other than what you'd expect
typeof(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.

Thanks,
Ilya




--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
R Graph Gallery: http://addictedtor.free.fr/graphiques
blog:            http://romainfrancois.blog.free.fr
|- http://bit.ly/xbKv0R : Crawling facebook with R
|- http://bit.ly/v3WB8S : ... And now for solution 17, still using Rcpp
`- http://bit.ly/uaQDGr : int64: 64 bit integer vectors for R
_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to