[Rcpp-devel] Garbage collection PreserveObject.

Romain Francois francoisromain at free.fr
Sun Jan 24 12:03:47 CET 2010


On 01/24/2010 01:06 AM, Dirk Eddelbuettel wrote:
>
> Houston, we have lift-off.  We have come full circle. Our use of preserve /
> release followed Simon's answers to your questions on r-devel.
>
> On 24 January 2010 at 00:44, Guillaume Yziquel wrote:
> | I recently noticed that the garbage-collection interfacing toping has
> | been quite active at the beginning of the month on this list, so I'm
> | asking here. My question concerns essentially my OCaml binding, but I'll
> | try to make it as language agnostic as possible to somehow stay within
> | the scope of this list.
>
> You are a nautical mile and a half off-topic for 'rcpp-devel', but we don't
> mind.  We only charge after the third question.  Just kidding.

Yes. In this list you only have access to a very limited subset of 
people following R-devel, so I'd still advise you to use R-devel ... 
unless maybe you want to rebase your binding on Rcpp ;-) but from I read 
on R-devel you seem to be willing to go deeper in the api than we are 
(we don't expose internal aspects of SEXPREC, etc ...)

> | Two questions:
> |
> | -1- From what I gathered, it is fine to issue two PreserveObject
> | statements on the same SEXP, and then two ReleaseObject afterwards. It
> | seemed to me that reference counting was not needed, from my
> | understanding of the past discussion. Am I wrong?
>
> I think you are right. We simply the object along, but AFAIK R just flips a
> bit:

That's not true. this does not modify the object itself, but attaches it 
to the precious list. The object then gains protection simply because it 
is part of the precious list. more details on the direct response to 
Guillaume, or the mentionned R-devel thread.

>      void preserve(){ if( m_sexp != R_NilValue ) R_PreserveObject(m_sexp) ; }
>      void release() { if( m_sexp != R_NilValue ) R_ReleaseObject(m_sexp) ; }
>
> | -2- Once you have, in your C/C++ code, obtained a pointer to a SEXP from
> | R, do you need to PROTECT it, then do a PreserveObject, then UNPROTECT
> | it? Or is PreserveObject simply fine? (I guess so, but I'm damn unsure).

It is plain simple. I think.

You need to make sure that the object is not subject of garbage 
collection. If you only use it locally (e.g. in a function) : 
PROTECT/UNPROTECT is fine are will be more efficient (see Simon's emails).

If your object needs to persist, then you just cannot use 
PROTECT/UNPROTECT, you have to do something else. Preserve/Release is 
just a shortcut for attaching the object to another object that you know 
for sure it is protected (the precious list), so your object gets 
protected as well.

There are other ways, you can create your own pairlist, or your own 
environment, and manage this manually.

Simon said that the most efficient way would be to allocate a big enough 
generic vector and manage it yourself. Is it worth the trouble ? maybe not.

Hope this helps. Romain

> More the latter, but it is complicated. We had replaced all PROTECT /
> UNPROTECT calls with preserve / release but we needed to go back for some
> reason.  Romain has the gory details.
>
> Dirk
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/KfKn : Rcpp 0.7.2
|- http://tr.im/JOlc : External pointers with Rcpp
`- http://tr.im/JFqa : R Journal, Volume 1/2, December 2009



More information about the Rcpp-devel mailing list