Skip to content

[Rcpp-devel] reference counting, garbage collection, etc ....

3 messages · Romain Francois, Dirk Eddelbuettel

#
Hi,

Right, so looking at R_PreserveObject and R_ReleaseObject (still waiting 
for some insight from R-devel) it seems that we can preserve and release 
the same SEXP more than once. preserve adds the sexp to the list of 
preserved SEXP (even if it is there already), and release release the 
first one it finds.

This leads me to thinking that we should make the garbage collection 
stuff automatic and hidden, i.e when setting the SEXP of an RObject, we 
automatically preserve it, and we release it automatically when the 
object goes out of scope.

Currently we have to manage this business manually, which will leads to 
problems.

So i propose that RObject::m_sexp becomes private (as opposed to 
protected) and we had a protected setSEXP that :
- release the current SEXP if there is one
- preserve the new SEXP

and make preserve and release protected (maybe even private members of 
RObject), and remove the preserved bool.
#
On 2 January 2010 at 13:37, Romain Fran?ois wrote:
| Right, so looking at R_PreserveObject and R_ReleaseObject (still waiting 
| for some insight from R-devel) it seems that we can preserve and release 
| the same SEXP more than once. preserve adds the sexp to the list of 
| preserved SEXP (even if it is there already), and release release the 
| first one it finds.
| 
| This leads me to thinking that we should make the garbage collection 
| stuff automatic and hidden, i.e when setting the SEXP of an RObject, we 
| automatically preserve it, and we release it automatically when the 
| object goes out of scope.

That was always the idea with Rcpp objects.  The classes manage memory, the
user doesn't have to worry.

And the design was always like this -- release memory in the dtor.
 
| Currently we have to manage this business manually, which will leads to 
| problems.

"We" as in class implementers/authors, or "we" as users?   As authors, yes we
do have to manage this, but that is because R forces us to, no?

| So i propose that RObject::m_sexp becomes private (as opposed to 
| protected) and we had a protected setSEXP that :
| - release the current SEXP if there is one
| - preserve the new SEXP
| 
| and make preserve and release protected (maybe even private members of 
| RObject), and remove the preserved bool.

Sounds like a technically more sounds implementation, but nothing really
changes for the user, no?  Ie there never was a user-accessibly method to 
unprotect/unpreserve.

Am I missing something?   

Dirk
#
On 01/02/2010 03:07 PM, Dirk Eddelbuettel wrote:
This is currently accesible: preserve, release, isPreserved and 
forgetPreserve are public member functions of RObject, that the user 
needs to use, because I did not get R_PreserveObject/ R_ReleaseObject 
right. see the documentation in RObject.h for these.

I'll hide them and work them out automatically.

Probably not until tonight, but we should release anyway. this is a 
subtle problem.