[Rd] External pointers and an apparent memory leak

From: James Bullard <jbullard_at_pacificbiosciences.com>
Date: Wed, 14 Sep 2011 21:21:25 +0000

I'm using external pointers and seemingly leaking memory. My determination of a memory leak is that the R process continually creeps up in memory as seen by top while the usage as reported by gc() stays flat. I have isolated the C code:

void h5R_allocate_finalizer(SEXP eptr) {

    Rprintf("Calling the finalizer\n");
    void* vector = R_ExternalPtrAddr(eptr);     free(vector);

SEXP h5R_allocate(SEXP size) {

    int i = INTEGER(size)[0];
    char* vector = (char*) malloc(i*sizeof(char));     SEXP e_ptr = R_MakeExternalPtr(vector, R_NilValue, R_NilValue);     R_RegisterCFinalizerEx(e_ptr, h5R_allocate_finalizer, TRUE);     return e_ptr;


If I run an R program like this:

v <- replicate(100000, {
  .Call("h5R_allocate", as.integer(1000000))


Then you can see the problem (top reports that R still has a bunch of memory, but R doesn't think it does). I have tried using valgrind and it says I have memory left on the table at the end lest you think it is because top. Also, I have tried Free/Calloc as well and this doesn't make a difference. Finally, I see this in both R-2-12 (patched) and R-2-13 - I think it is more an understanding issue on my part.

thanks much in advance, to me it really resembles the connection.c code, but what am I missing?

thanks, jim

        [[alternative HTML version deleted]]

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 15 Sep 2011 - 11:47:30 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 15 Sep 2011 - 17:40:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive