[Rd] Finalization and external pointers

Luke Tierney luke at stat.uiowa.edu
Mon May 3 18:27:02 CEST 2004

On Mon, 3 May 2004, Duncan Murdoch wrote:

> I'm adding things to the Windows RGui so that there's more control of
> the interface from within R.
> One thing I'm considering is giving access to the Graphapp window
> objects using external pointers.  This raises the issue of
> finalization on both sides:
>  - If someone creates a pointer referring to a window, then that
> pointer should be changed to NULL when the window is closed.
>  - If garbage collection destroys a pointer referring to a window,
> then the window should know not to change that pointer to NULL later.
> Are there other examples like this I can look at?  I'd like to follow
> existing conventions rather than invent my own.
> And a related question:  is there a writeup anywhere on the
> R_RegisterFinalizerEx function?  What does the onexit argument do?

I added some notes I have on weak references and finalization at


The notes are a bit old but I think still apply, such as they are.

The onexit argument sets a flag; when R exits normally it will attempt
to run the finalizers of all references with this flag set.

The simple examples in these notes may be of use, or not.  The Haskell
reference may also be worth a look as one writeup of the issues.

Making sure you have the right match of object lifetimes with object
identities is probably the trickiest issue--do you need to make sure
two ways of asking for an R reference to the same physical window
return the same R pointer object? Probably you do; that will affect
the design in that the pointer object you use has to exist as long as
the physical window does.  Also keep in mind that these things could
get saved in a workspace (they will be restored with NULL pointers).



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

More information about the R-devel mailing list