<div dir="ltr">Dirk, good point. This is a very simple function and if I add complexity at the end there's no significant difference between calling -fun-, -cppFuncall- and -RcppFuncall-. Just to make this thread more complete, if I modify -fun- from<div><br></div><div><div>fun <- function(x) {</div><div>  -cos(x[1])*cos(x[2])*exp(-((x[1] - pi)^2 + (x[2] - pi)^2))</div><div>}</div><div><br></div><div>to</div><div><br></div><div><div>fun <- function(x) {</div><div>  w<-sapply(1:1e3,function(x) -cos(x[1])*cos(x[2])*exp(-((x[1] - pi)^2 + (x[2] - pi)^2)))</div><div>  1</div><div>}</div></div><div><br></div><div>Running the benchmark I get</div><div><br></div><div><div>Unit: relative</div><div>                expr min lq     mean median uq max neval</div><div>  cppFuncall(x, fun)   1  1 1.044215      1  1 6.1  1000</div><div> RcppFuncall(x, fun)   1  1 1.018957      1  1 2.7  1000</div><div>              fun(x)   1  1 1.000000      1  1 1.0  1000</div></div><div><br></div><div>Which is way more reasonable from what I was getting at first. No significant difference overall.</div><div><br></div><div>Kevin, thanks for the example, now I get why isn't a good idea! This has been very useful to me.</div><div><br></div><div>Best,</div></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>George G. Vega Yon<br>+1 (626) 381 8171<br><a href="http://www.its.caltech.edu/~gvegayon/" target="_blank">http://www.its.caltech.edu/~gvegayon/</a></div></div></div></div>
<br><div class="gmail_quote">On Wed, Aug 3, 2016 at 12:34 PM, Kevin Ushey <span dir="ltr"><<a href="mailto:kevinushey@gmail.com" target="_blank">kevinushey@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The simplest demonstrating example I can think of:<br>
<br>
---<br>
<br>
#include <Rcpp.h><br>
using namespace Rcpp;<br>
<br>
struct A { ~A() { Rprintf("~A()"); } };<br>
<br>
// [[Rcpp::export]]<br>
void ouch() {<br>
  A a;<br>
  Rf_error("ouch!");<br>
}<br>
<br>
/*** R<br>
ouch()<br>
*/<br>
<br>
---<br>
<br>
Call 'Rcpp::sourceCpp()' on that and you'll see:<br>
<br>
> Rcpp::sourceCpp('~/Desktop/Untitled.cpp')<br>
> ouch()<br>
 Error in ouch() : ouch!<br>
<br>
Note that the destructor was not called. Replace `Rf_error` with<br>
`Rcpp::stop` and you will see the destructor is called.<br>
<br>
It's possible that you won't have a memory leak per-se (if the memory<br>
is all allocated on the stack, maybe the runtime still knows to just<br>
clear the entire stack after something like this) but not running<br>
destructors is definitely a big problem.<br>
<br>
Cheers,<br>
Kevin<br>
<div class="HOEnZb"><div class="h5"><br>
On Wed, Aug 3, 2016 at 12:22 PM, Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>> wrote:<br>
><br>
> On 3 August 2016 at 11:38, George Vega Yon wrote:<br>
> | Thanks for the quick reply! What kind of errors are we talking about? I a new<br>
> | run I explicitly caused an error by passing a character vector, and had no<br>
> | memory leak (using Valgrind):<br>
> |<br>
> | cppFuncall(letters, fun)<br>
> | Error in cos(x[1]) : non-numeric argument to mathematical function<br>
> |<br>
> | If its not too much to ask, could you give an explicit example in which that<br>
> | happens (memory leak)? Just trying to learn here!<br>
><br>
> You are misreading what Kevin said.  You short ten-line example runs fine.<br>
> We are not saying it has an error.<br>
><br>
> What Kevin explained to you is that in the context of larger programs,<br>
> possibly with inputs you don't know yet, some errors may occur. And both<br>
> Rcpp:Function() and Rcpp::stop() can recover from that.<br>
><br>
> Your example cannot.  So by all means use it as a small (local) script if the<br>
> few milliseconds matter to you.  But think twice about using it in a larger<br>
> context, or about promoting it as a general solution, and understand why we<br>
> can't put it into Rcpp as is.<br>
><br>
> Hth, Dirk<br>
><br>
> --<br>
> <a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
</div></div></blockquote></div><br></div>