[Rd] segfault following a detach

Izmirlian, Grant (NIH/NCI) [E] izmirlig at mail.nih.gov
Sat Dec 10 01:28:34 CET 2005


Jim:

This reminds me of problems I've had before, but usually they occur when I quit R
i.e. q(), because when testing and developing I can't remember actually detaching
a package. I can however think of countless times I get a segmentation fault upon 
quiting R. Usually this boils down to a hidden return argumen that is given an
insufficient allocation of memory. For example

  "foo" <- function(x, y, z){
     nx <- length(x)
     ny <- length(y)
     nz <- length(z)
     ans <- .C("bar",
            x = as.double(x),
            y = as.double(y),
            z = as.double(z),
            res1 = as.double(rep(0, nx)),
            res2 = as.double(rep(0, nx*ny)),
            PACKAGE = "FooBar")
     list(result = asn$res1)
}

Notice that only ans$res1 is returned so that it is easy to forget about ans$res2, 
as I have often done! Now suppose that the C routine actually needs nx*ny*nz space 
(say) for the pointer to double at the position indicated by res2 instead of just 
the nx*ny provided. Although you would expect a segmentation fault at runtime, it 
is my experience that sometimes the function completes and the segmentation fault 
doesn't happen until I quit R.

I hope that these comments are helpful,

Grant Izmirlian,
NCI



More information about the R-devel mailing list