[Rcpp-devel] How to free memory in Rcpp
Yan Zhou
zhouyan at me.com
Fri Dec 14 04:54:17 CET 2012
You really are not reading the list as close as you should be. If you want advice, then take it when people give it to you or stop asking.
AFAIK, your code does not involve manually allocated memory, all memories are managed by RAII objects, I don't see where the question of "freeing memory" come from. And the error message suggests nothing about memory problems. So either you have no idea of C++ at all or you are just asking some random question and hope others to debug your code. The later is not going to happen.
On Dec 14, 2012, at 3:25 AM, Honglang Wang <wanghonglang2008 at gmail.com> wrote:
> I am sorry that I was abusing this list. My point is
> 1) First, this code works for me for some simulation cases. I repeated the simulation 1000 times, which then called this Rcpp function 1000 times at least. And this code worked perfectly.
Computers don't do random things, unlike human beings. Something worked once, is very likely to work whatever times you repeat it as long as the input is the same (unless the function has side effect). So repeating it 1000 times is the same as once.
> 2) But in some other simulation case (here what I mean by simulation case, just change some parameters to produce the data, but the data formats are the same.), when I repeated the simulation 1000 times, at some point, for example, at the 298th simulation, it did not work. Then in order to see what is the problem, I just did the 298th simulation separately (since I set the seed, it used the same data), but it worked well in this case. Then I really have no idea what is going on there.
Since you already find an input that crash the program, you should go through the function to see where exactly the crash happens. For example, print out the intermediate steps in both R and C++ side to see where the execution stopped. Using a debugger is better. Sometime it takes a great time to trace back the crash site. Debugging can be exhausting. But it is supposed to be done by YOU. You cannot throw a bunch of code to the list, ask a non-sense question, and hope others to spend time to debug it for you.
Here are a few things people usually do before asking in a mailing list (not just Rcpp list, any such lists like R-help, StackOverflow, etc).
1. I write a program, it crashes,
2. I find out the site of crash
3. I make the program simpler and simpler until it is minimal and the crash is now reproducible.
4. I still cannot figure out what is wrong with that four or five lines that crash the minimal example
5. I ask
Ask yourself, have you go through the first 4 steps? It does not matter how stupid your questions are. We all asked silly questions before, that is how we learn. But it matters you put in effort to ask the right question. The more effort you put into it, the more specific question you ask and more helpful answers you get. I suspect you don't know how to do that at all. So if I were you, I will take Dirk's advice, take a deep breath, go for a walk, and start learning C++.
Yan
>
> 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 10:06 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> Honglang,
>
> You are now abusing the free facilities offered by this list. Your examples
> are not self-contained [ no sample data or simulated data ] and hence not
> reproducible. Your code is complex, and not motivated. And we have told you
> REPEATEDLY to shorten your code to chunks until it works.
>
> On 13 December 2012 at 21:15, Honglang Wang wrote:
> | I have no idea what's this error.
>
> My best guess is that we already explained this to you, but you are not
> really reading our replies all that closely. Here is a hint: repeatedly
> calling an R function is probably not the smartest thing to do in an
> otherwise complex and hard to decipher program. [1]
>
> | I am just wondering whether I need to free some memory in this code.
>
> You are also making it pretty clear that you don't really know what you are
> doing with C++, or else you would not ask this question (and the earlier
> email people were to polite to reply to).
>
> I suggest that you take a deep breath, go for a walk and once you're back,
> start learning about C++.
>
> So please stop emailing the list unless you have something more substantial
> to post or ask -- and follow common courtesy and list etiquette.
>
> Right now you are wasting your time, as well as the time of a few hundred
> list subscribers. Stop now.
>
> Dirk
>
>
> [1] Just to be plain: Calling a function repeatedly is not per se a bad
> thing. Complete example below.
>
> -----------------------------------------------------------------------------
> #include <Rcpp.h>
>
> using namespace Rcpp;
>
> // [[Rcpp::export]]
> double foo(Function f, NumericVector x) {
> double r = 0.0;
> for (int i=0; i<100; i++)
> r += as<double>(f(x));
> return r;
> }
>
>
> /*** R
> x <- 1:10
> foo(mean, x)
> ***/
> -----------------------------------------------------------------------------
>
> Here is the result from the 100 calls:
>
> R> sourceCpp("/tmp/fun.cpp")
>
> R> x <- 1:10
>
> R> foo(mean, x)
> [1] 550
> R>
>
>
> As expected...
>
>
>
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> 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/20121214/9c3a4aa1/attachment-0001.html>
More information about the Rcpp-devel
mailing list