<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>
Just wanted to confirm that my code returning a list containing wrapped primitives appears to work correctly now. Sorry for the delayed reply -- I am not a SVN user, so I had to wait for the official release before validating. Thanks!<div><br></div><div>Ilya</div><div><br></div><br><div><div id="SkyDrivePlaceholder"></div>> Date: Sat, 7 Apr 2012 15:19:27 +0200<br>> From: romain@r-enthusiasts.com<br>> To: rcpp-devel@lists.r-forge.r-project.org<br>> CC: bbc5019@live.com<br>> Subject: Re: [Rcpp-devel] Rcpp Lists and memory corruption (resend)<br>> <br>> Hello,<br>> <br>> rev 3565 should address this issue. Thanks again for the bug report.<br>> <br>> This was indeed due to SEXP not being protected sufficiently. <br>> List::create allocates a new object, which might cause the GC to collect <br>> one of the unprotected SEXP passed in.<br>> <br>> Let me know if this works for you.<br>> <br>> Romain<br>> <br>> Le 31/03/12 11:35, Romain Francois a écrit :<br>> > Some head scratching this morning.<br>> ><br>> > If I have this:<br>> > return Rcpp::List::create(Rcpp::Named( "testVar" ) = 10 ) ;<br>> ><br>> > instead of:<br>> > return Rcpp::List::create(Rcpp::Named( "testVar" ) = wrap(10) ) ;<br>> ><br>> > everything runs smoothly.<br>> ><br>> ><br>> ><br>> > So my guess is that the SEXP that is made in the wrap call does not get<br>> > sufficient protection.<br>> ><br>> > Not sure this is an Rcpp bug or a user bug, but there might be somethign<br>> > I can do to prevent it.<br>> ><br>> > Romain<br>> ><br>> ><br>> > Le 31/03/12 09:20, Ilya E a écrit :<br>> >> (Apologies for sending this twice -- the formatting on the first email<br>> >> got completely mangled)<br>> >><br>> >><br>> >> Are there any known issues regarding memory corruption caused by lists<br>> >> created inside of Rcpp? I started noticing some inconsistencies in the<br>> >> data returned by my app, and was able to reduce the issue to the<br>> >> following reproducible example. It seems to suggest that lists created<br>> >> in Rcpp and returned to R can cause sporadic memory corruption. See<br>> >> code below for a demonstration:<br>> >><br>> >> require(inline)<br>> >> require(Rcpp)<br>> >><br>> >> testFun <- local( {<br>> >> fun <- cxxfunction(<br>> >> signature(), '<br>> >> return Rcpp::List::create(Rcpp::Named( "testVar" ) = wrap(10));<br>> >> ', plugin = "Rcpp" )<br>> >><br>> >> function(){<br>> >> fun()<br>> >> }<br>> >> } )<br>> >><br>> >> # generate the result set -- you would expect a numeric vector that<br>> >> consists of repetition of the value 10<br>> >> testOutcome = mapply(<br>> >> function(i) {<br>> >> fTouch = testFun()<br>> >> return(fTouch$testVar)<br>> >> },<br>> >> 1:5612062)<br>> >><br>> >> # The result ensd up being something other than what you'd expect<br>> >> typeof(testOutcome)<br>> >> for (i in 1:length(testOutcome)) {<br>> >> if (testOutcome[[i]] != 10) {<br>> >> cat(i, "\n")<br>> >> }<br>> >> }<br>> >><br>> >> You'll notice two things here. First, mapply fails to coerce the<br>> >> returned results to a vector (since in some cases the return from<br>> >> testFun seems to have more than 1 element) and second, not all return<br>> >> values are equal to 10, as you would expect. The actual position in<br>> >> the vector of the corrupted data is sporadic. If you keep running this<br>> >> over and over again, you'll see the wrong value in different places.<br>> >> Sometimes you can even get R to crash after multiple runs. On rare<br>> >> occasions there will be no errors at all. For some reason you tend to<br>> >> see the most errors on the first run, especially if you open a new<br>> >> instance of R.<br>> >><br>> >> If you replace the line "fTouch = testFun()" with a native call to R<br>> >> "fTouch = list(testVar = 10)" then everything works fine. Also<br>> >> returning a NumericVector instead of a list also works fine -- the<br>> >> issue seems to appear after encapsulating the return value in a list<br>> >> inside of Rcpp.<br>> >><br>> >> I am using R 2.14.2 on Win32& Win64. Rcpp version 0.9.10.<br>> >><br>> >> Thanks,<br>> >> Ilya<br>> ><br>> <br>> <br>> -- <br>> Romain Francois<br>> Professional R Enthusiast<br>> +33(0) 6 28 91 30 30<br>> R Graph Gallery: http://addictedtor.free.fr/graphiques<br>> blog: http://romainfrancois.blog.free.fr<br>> |- http://bit.ly/xbKv0R : Crawling facebook with R<br>> |- http://bit.ly/v3WB8S : ... And now for solution 17, still using Rcpp<br>> `- http://bit.ly/uaQDGr : int64: 64 bit integer vectors for R<br></div> </div></body>
</html>