<div>Hi Dirk,</div><div><br></div><div>Does the code crash your R every time you run it or only occasionally? I ran your sample code exactly the way it's written, and my R did not crash. </div><div><br></div><div><div>
> myFun <- cppFunction('NumericMatrix myFun(NumericMatrix input, int n){</div><div>+ NumericMatrix A(n, n);</div><div>+ for(int Row = 0; Row < n; Row++) {</div><div>+ for(int Col = 0; Col < n; Col++) {</div>
<div>+ A(Row, Col) = input(Row, Col);</div><div>+ }</div><div>+ }</div><div>+ return A;</div><div>+ }')</div><div>> </div><div>> n <- 10</div><div>> x <- 1:n^2</div><div>> N <- 1e6</div>
<div>> b <- 0</div><div>> for (j in 1:N) {</div><div>+ means <- matrix(x, n, n)</div><div>+ res <- myFun(means, n)</div><div>+ a <- res[1, 1]</div><div>+ b <- b + a</div><div>+ }</div>
<div>> cat(sprintf("Done, b is %d\n", b))</div><div>Done, b is 1000000</div><div>> </div></div><div><br></div><br>
<br><br><div class="gmail_quote">On Thu, May 16, 2013 at 11:02 AM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Here is the self-contained example I asked for.<br>
<br>
And yes, it crashes for me too. So let's not create 1e6 temp matrices.<br>
Until someone has time to debug memory management internals. Which is really<br>
hard, so this may not get fixed for a while. Sorry.<br>
<br>
<br>
#!/usr/bin/Rscript<br>
<br>
library(Rcpp)<br>
myFun <- cppFunction('NumericMatrix myFun(NumericMatrix input, int n){<br>
<div class="im"> NumericMatrix A(n, n);<br>
for(int Row = 0; Row < n; Row++) {<br>
for(int Col = 0; Col < n; Col++) {<br>
A(Row, Col) = input(Row, Col);<br>
}<br>
}<br>
return A;<br>
</div>}')<br>
<div class="im"><br>
n <- 10<br>
x <- 1:n^2<br>
</div>N <- 1e6<br>
b <- 0<br>
for (j in 1:N) {<br>
<div class="im"> means <- matrix(x, n, n)<br>
</div> res <- myFun(means, n)<br>
<div class="im"> a <- res[1, 1]<br>
</div> b <- b + a<br>
}<br>
<br>
cat(sprintf("Done, b is %d\n", b))<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
--<br>
Dirk Eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</div></div></blockquote></div><br>