Le 14/12/12 03:15, Honglang Wang a écrit :
Hi,
Here is the sample code:
// [[Rcpp::export]]
List betahat(Function ker, double t0, NumericMatrix Xr, NumericMatrix
yr, NumericVector tr, double h, int m) {
int n = Xr.nrow(), p = Xr.ncol();
arma::mat X(Xr.begin(), n, p, false);
arma::mat y(yr.begin(), n, 1, false);
arma::colvec t(tr.begin(), tr.size(), false);
arma::mat T = X;
T.each_col() %= (t-t0)/h;
arma::vec K =as<arma::vec>(ker(tr-t0,h))/
m;
double L1 = arma::accu(K%X.col(0)%X.col(0));
double L2 = arma::accu(K%X.col(0)%X.col(1));
double L3 = arma::accu(K%X.col(1)%X.col(1));
double L4 = arma::accu(K%X.col(0)%T.col(0));
double L5 = arma::accu(K%X.col(1)%T.col(0));
double L6 = arma::accu(K%X.col(1)%T.col(1));
double L7 = arma::accu(K%T.col(0)%T.col(0));
double L8 = arma::accu(K%T.col(0)%T.col(1));
double L9 = arma::accu(K%T.col(1)%T.col(1));
double R1 = arma::accu(K%X.col(0)%y);
double R2 = arma::accu(K%X.col(1)%y);
double R3 = arma::accu(K%T.col(0)%y);
double R4 = arma::accu(K%T.col(1)%y);
arma::mat L(2*p,2*p);
L(0,0)=L1;L(0,1)=L2;L(0,2)=L4;L(0,3)=L5;
L(1,0)=L2;L(1,1)=L3;L(1,2)=L5;L(1,3)=L6;
L(2,0)=L4;L(2,1)=L5;L(2,2)=L7;L(2,3)=L8;
L(3,0)=L5;L(3,1)=L6;L(3,2)=L8;L(3,3)=L9;
arma::mat R(2*p,1);
R(0,0)=R1;R(1,0)=R2;R(2,0)=R3;R(3,0)=R4;
arma::vec betahat = arma::solve(L,R);
arma::colvec betahat0(betahat.begin(),betahat.size()/2,false);
return List::create(Named("betahat") = betahat0);
}
I will call this function repeatedly, and at some point, it went wrong
with the following error:
Error in betahat(ker, x, X, y, t, h, m) :
promise already under evaluation: recursive default argument
reference or earlier problems?
Calls: system.time ... apply -> FUN -> betahat -> .External -> cpp_exception
Execution halted
I have no idea what's this error. I am just wondering whether I need to
free some memory in this code. Thanks.
You don't. The explicit constructor/destructor scheme of C++ takes care
of this automatically for you.
So there is a chance you are victim of a well hidden bug. These are
usually related to garbage collection and us not making enough
precaution somewhere. This is very hard to track.
As a wild guess, I think it is because Function return you an
unprotected SEXP. It does not matter most of the time because you
consume it, but sometimes the GC will run and collect your object before
as<mat> has a chance to process it.
If my guess is correct, then doing yourself some protection might do the
trick, something like:
NumericVector result = ker(tr-t0,h) ;
arma::vec K =as<arma::vec>(result)/m;
will do the trick since the result of the function call then gets
protected by the "result" object.
If this does not work, then you can do some debugging as indicated by
others.
Romain
Best wishes!
Honglang Wang
Office C402 Wells Hall
Department of Statistics and Probability
Michigan State University
1579 I Spartan Village, East Lansing, MI 48823
wangh...@msu.edu <mailto:wangh...@msu.edu>
On Thu, Dec 13, 2012 at 4:57 PM, <rom...@r-enthusiasts.com
<mailto:rom...@r-enthusiasts.com>> wrote:
Hello,
What memory do you want to free ? Can you give an example of code
where there is memory you'd like to "free" ?
Romain
Le 2012-12-13 22:52, Honglang Wang a écrit :
Dear All,
How to free memory in Rcpp? What's the command? Thanks.
Best wishes!
Honglang Wang
Office C402 Wells Hall
Department of Statistics and Probability
Michigan State University
1579 I Spartan Village, East Lansing, MI 48823
wangh...@msu.edu <mailto:wangh...@msu.edu> [1]
Links:
------
[1] mailto:wangh...@msu.edu <mailto:wangh...@msu.edu>
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
R Graph Gallery: http://gallery.r-enthusiasts.com
blog: http://romainfrancois.blog.free.fr
|- http://bit.ly/RE6sYH : OOP with Rcpp modules
`- http://bit.ly/Thw7IK : Rcpp modules more flexible
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel