From luke at stat.uiowa.edu  Mon Nov  1 19:31:14 2004
From: luke at stat.uiowa.edu (Luke Tierney)
Date: Mon Nov  1 19:32:19 2004
Subject: [Rd] Destructive str(...)?
In-Reply-To: <Pine.LNX.4.44.0410311003570.11787-100000@gannet.stats>
References: <Pine.LNX.4.44.0410311003570.11787-100000@gannet.stats>
Message-ID: <Pine.LNX.4.58.0411011227110.2370@nokomis.stat.uiowa.edu>

On Sun, 31 Oct 2004, Prof Brian Ripley wrote:

> Just to be 100% clear, the finalizer is called *at most* once if (as in
> tcltk) R_RegisterCFinalizer is called.  If you want it to be called
> exactly once, you need to use R_RegisterCFinalizerEx.
> 
> The issue is that there may not be a final gc().
> 
> BTW, str(x) is destructive here too, so we do need to improve str().
> I have code written, but access to svn.r-project.org is down (yet again).
> 
> > x <- as.tclObj(pi)
> > str(x)
> Class 'tclObj' length 1 <pointer: 0x860c3f8>
> > str(x)
> length 1 <pointer: 0x860c3f8>
> 

Improving str is a good idea, but as there are other uses of unclass
out there it would probably be best to change the implementation to
wrap the pointers rather than use them directly.

In hindsight it would probably have been better to use an
implementation that internally wraps esxernal pointers as well as
environments so only the bits that really do need reference behavior
get it, and maybe at some point we should consider doing that.  Name
objects should probably just disallow changing attributes as null
currently does.

luke


> 
> On 31 Oct 2004, Peter Dalgaard wrote:
> 
> > Simon Urbanek <simon.urbanek@math.uni-augsburg.de> writes:
> > 
> > > Now, hold on a second - I thought the main point of EXTPTR is that the
> > > finalizer is called only once, that is when the last instance of the
> > > reference is disposed of by the gc (no matter how many copies existed
> > > meanwhile). Am I wrong and/or did I miss something? I did some tests
> > > which support my view, but one never knows ...
> > 
> > How do you ensure that the finalizer is called once? By *not* copying
> > the reference object! You can have as many references to it as you
> > like (i.e. assign it to multiple variables), and the object itself is
> > not removed until the last reference is gone, but if you modify the
> > object (most likely by setting attributes, but you might also change
> > the C pointer payload in a C routine), all "copies" are changed:
> > 
> > > x <- as.tclObj(pi)
> > > x
> > <Tcl> 3.14159265359
> > > y <- x
> > > y
> > <Tcl> 3.14159265359
> > > mode(x)
> > [1] "externalptr"
> > > attr(x, "Simon") <- "Urbanek"
> > > attributes(y)
> > $class
> > [1] "tclObj"
> > 
> > $Simon
> > [1] "Urbanek"
> 
> 

-- 
Luke Tierney
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
   Actuarial Science
241 Schaeffer Hall                  email:      luke@stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu


[Rd] Destructive str(...)?
Luke Tierney