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);
    R_ClearExternalPtr(eptr);
}

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))
})
rm(v)
gc()

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]]

