[Rd] external pointers

Byron Ellis ellis at stat.harvard.edu
Sat Dec 10 01:23:55 CET 2005


use a C finalizer...

void MyObject_finalize(SEXP opaque) {
	MyObject *obj = (MyObject*)R_ExternalPtrAddr(opaque);
	if(NULL != obj) delete obj;
}

and in your setup code...

PROTECT(p = R_MakeExternalPtr(...));
R_RegisterCFinalizer(p,MyObject_finalize);






On Dec 9, 2005, at 3:04 PM, Ross Boylan wrote:

> I have some C data I want to pass back to R opaquely, and then back to
> C.  I understand external pointers are the way to do so.
>
> I'm trying to find how they interact with garbage collection and  
> object
> lifetime, and what I need to do so that the memory lives until the
> calling R process ends.
>
> Could anyone give me some pointers?  I haven't found much  
> documentation.
> An earlier message suggested looking at simpleref.nw, but I can't find
> that file.
>
> So the overall pattern, from R, would look like
> opaque <- setup(arg1, arg2, ....)  # setup calls a C fn
> docompute(arg1, argb, opaque)  # many times. docompute also calls C
> # and then when I return opaque and  the memory it's wrapping get
> #cleaned up.  If necessary I could do
> teardown(opaque)  # at the end
>
> "C" is actually C++ via a C interface, if that matters.  In  
> particular,
> the memory allocated will likely be from the C++ run-time, and  
> needs C++
> destructors.
>
> -- 
> Ross Boylan                                      wk:  (415) 514-8146
> 185 Berry St #5700                               ross at biostat.ucsf.edu
> Dept of Epidemiology and Biostatistics           fax: (415) 514-8150
> University of California, San Francisco
> San Francisco, CA 94107-1739                     hm:  (415) 550-1062
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

---
Byron Ellis (ellis at stat.harvard.edu)
"Oook" -- The Librarian



More information about the R-devel mailing list