[Rcpp-devel] How to free memory in Rcpp

Honglang Wang wanghonglang2008 at gmail.com
Fri Dec 14 03:15:38 CET 2012


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.

Best wishes!

Honglang Wang

Office C402 Wells Hall
Department of Statistics and Probability
Michigan State University
1579 I Spartan Village, East Lansing, MI 48823
wangho16 at msu.edu



On Thu, Dec 13, 2012 at 4:57 PM, <romain at 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
>> wangho16 at msu.edu [1]
>>
>>
>> Links:
>> ------
>> [1] mailto:wangho16 at msu.edu
>>
>
> ______________________________**_________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-**project.org<Rcpp-devel at lists.r-forge.r-project.org>
> https://lists.r-forge.r-**project.org/cgi-bin/mailman/**
> listinfo/rcpp-devel<https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121213/937db2f8/attachment.html>


More information about the Rcpp-devel mailing list