[R] Problems with .C - [Ff]ree()?

Mark White mjw at celos.net
Tue Jan 15 03:02:53 CET 2002


Thomas Lumley writes:
> Very Strange.  It does sound suspiciously as though you are using pointers
> after they have been free()d, but I don't see how the upgrade would have
> triggered this -- I assume you upgraded from 1.3.x, not from something
> ancient.

It was indeed 1.3.something.  I agree that it sounds like a
memory leak in my code, but... I just tried compiling the
following C library routine (based on the arguments of a
file-reading function that's causing problems):

    void R_test (char **filename, int *dim, Rcomplex *matrix)
    {
        double *ptr;
        ptr = (double*) malloc(sizeof(double) * 25600);
        free(ptr);
    }

which, when called like this from within my R function

    .C("R_test",as.character(filename),as.integer(adimi),
        as.complex(im))

produces a segfault in Rf_duplicate soon after returning for
the somethingth time.  If I make replace the last argument
with something like complex(5) - rather smaller than im  - I
don't get an crash.  Likewise I don't get a crash if I just
repeatedly call R_test from a simple command-line for(){}.

Is there something horribly wrong with my use of .C above?

> Do you get segfaults running R CMD check on the survival package? This
> uses Calloc/Free quite a bit (probably so do other base/recommended
> packages).

I've just tried this; it ran fine.

Mark <><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list