Skip to content
Prev 55597 / 63424 Next

SIGSEGV in R_RunWeakRefFinalizer, object allocated with Rcpp

R's dyn.unload() will unconditionally unload the given shared object; it 
does not check whether there is any object (external pointer or weak 
reference) with a C finalizer pointing into the space of the shared 
object being unloaded. So it is expected that R will segfault later when 
such finalizer is run.

Currently there is no other way than to handle this on the side of the 
shared library/package, e.g. as Luke does in his simplemmap package. A 
library can declare R_unload_XXX function (see Writing R Extensions) 
that will be called before it is unloaded. So, one can keep say a list 
of objects with finalizers and clear/run the finalizers in R_unload_XXX.

I don't think we could do this automatically on the R's side with the 
current API. I don't think there is a portable way to check if a given C 
pointer is from a given loaded shared object (non-portable way on Linux 
might be scanning the maps in the /proc filesystem). Also, doing this 
for all objects with finalizers when unloading any library would 
probably be slow. Adding some API to aid libraries in doing the 
housekeeping is possible in principle.

So to answer your original question, this could probably be handled in 
Rcpp, but in either case I would not use dyn.unload() in the first 
place. This problem may be just one of many.

Best
Tomas
On 6.8.2018 17:35, I?aki ?car wrote: