From ripley at stats.ox.ac.uk  Sun Oct 31 11:20:45 2004
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Sun Oct 31 11:20:59 2004
Subject: [Rd] Destructive str(...)?
In-Reply-To: <x2r7nfkxz9.fsf@biostat.ku.dk>
Message-ID: <Pine.LNX.4.44.0410311003570.11787-100000@gannet.stats>

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>


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"

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] Destructive str(...)?
Prof Brian Ripley