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

Mark White mjw at celos.net
Wed Jan 16 13:45:36 CET 2002


Mark White writes:
> I also tried this: no problems for me with this small
> example, either, only dies when called from my R library
> code.  Maybe R needs to be doing something specific between
> the .C calls?  I'll see if I can extract a complete chunk of
> R code from my library which repeatably causes problems.

Ok, here's a pair which repeatably causes problems on this
machine.  I've spent a bit of time removing lines that don't
have any effect, but I expect it could be minimized further.

I've now tried on two other machines (one sparc, one other
NetBSD/i386 but with a different processor and release) and
it doesn't fail on either of them; and given the error
message, which I hadn't seen before, I'm starting to get
suspicious of the system's free() call.  However, here they
are, for posterity.

Mark <><

--- R_test.c

#include <stdio.h>
#include <stdlib.h>
#include "R.h"

void R_test (char **filename, int *dim, double *matrix)
{
	double *ptr;
	ptr = (double*)malloc(sizeof(double) * (160*160));
	if (ptr==NULL) {
		fprintf(stderr,"Ack\n");
		exit(0);
	}
	free(ptr);
}

--- R_test.R

Rtest2 <-
	function (arange) {
		im3d <- array(complex(160*160*14), c(160,160,length(arange)))
		j <- 0
		for (i in arange) {
			.C("R_test",as.character("test"),integer(100),double(160*160))
			j <- j+1
			im2d <- complex(160*160)
			im3d[,,j] <- im2d
		}
		return(im3d)
	}


Rtest <-
	function () {
		elem <- list()
		elem[[5]] <- array(double(1), c(256,160,14))
		for (i in 1:4) {
			elem[[i]] <- Rtest2(1:14)
		}
	}

--- session

$ R CMD SHLIB R_test.c

then, in a brand new R session with no saved data loaded:

> dyn.load("R_test.so")
> source("R_test.R")
> Rtest()
R.bin in free(): warning: junk pointer, too high to make sense.
zsh: segmentation fault (core dumped)  R

--- stack trace on the crash

Program received signal SIGSEGV, Segmentation fault.
0x80b4f83 in Rf_allocVector ()
(gdb) where
#0  0x80b4f83 in Rf_allocVector ()
#1  0x808779e in CPtrToRObj ()
#2  0x808c8cc in do_dotCode ()
#3  0x80972a4 in Rf_eval ()
#4  0x8098298 in do_begin ()
#5  0x80970fe in Rf_eval ()
#6  0x8097e90 in do_for ()
#7  0x80970fe in Rf_eval ()
#8  0x8098298 in do_begin ()
#9  0x80970fe in Rf_eval ()
#10 0x809766b in Rf_applyClosure ()
#11 0x809731a in Rf_eval ()
#12 0x80985d1 in applydefine ()
#13 0x8098a1f in do_set ()
#14 0x80970fe in Rf_eval ()
#15 0x8098298 in do_begin ()
#16 0x80970fe in Rf_eval ()
#17 0x8097e90 in do_for ()
#18 0x80970fe in Rf_eval ()
#19 0x8098298 in do_begin ()
#20 0x80970fe in Rf_eval ()
#21 0x809766b in Rf_applyClosure ()
#22 0x809731a in Rf_eval ()
#23 0x80b02d9 in R_ReplConsole ()
#24 0x80b0a46 in run_Rmainloop ()
#25 0x80b0a60 in Rf_mainloop ()
#26 0x81099c9 in main ()
#27 0x80582d0 in ___start ()

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