Re: [Rd] external pointers

From: Byron Ellis <ellis_at_stat.harvard.edu>
Date: Sat 10 Dec 2005 - 00:23:55 GMT

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@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@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

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

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Sat Dec 10 11:28:03 2005

This archive was generated by hypermail 2.1.8 : Sat 10 Dec 2005 - 02:21:31 GMT