[R] Destructor for S4 objects?

McGehee, Robert Robert.McGehee at geodecapital.com
Sat Jan 8 22:24:49 CET 2005


Adam, I'm about to encounter a very similar problem, so would be curious
if you find a good solution, although your thoughts with reg.finalizer
look promising. The thoughts I had sketched out for tackling this
problem:

1) Have all ancillary resources freed when the package is detached. The
new() function would create the object, allocate additional resources,
and store some kind of handle for these resources in the package
environment as a cleanup object. The cleanup object is then referenced
when .Last.lib() is run, and all ancillary resources are deallocated.

2) Write methods for rm() / remove() that cleanup the resources at the
same time as removing the object from the environment. A handle to these
resources would have to be stored either with the object like you
suggested, or in a cleanup object (like #1) that stores the handles of
every object needing cleanup. 

Conceptually, #2 seems very similar to your suggestion of using
reg.finalizer. However, it had seemed to make more sense (to me) to
deallocate resources at rm() time rather than gc() time, as I had
thought that garbage collection happened at R's convenience, rather than
when the user explicitly removes the object from the environment (which
is presumably garbage collected sometime later). However, any
information about the mechanics of removal and garbage collection, and
the correct way to cleanup objects is greatly appreciated, as I had
never quite understood when reg.finalizer should be used.

As a corollary, I have some concern that if R quits, then the garbage
collector never runs (and rm() and .Last.lib() as well). The resources
in my case are database objects that may need to be altered or deleted
after the corresponding R object is removed (or R quits), and I have yet
to find any solution that runs at quit time (although maybe rewriting
the q() function might not be a bad idea, although I'm hesitant to
rewrite base code). Let me know if you come up with anything more
elegant.

Best,
Robert

-----Original Message-----
From: Adam Lyon [mailto:lyon at fnal.gov] 
Sent: Friday, January 07, 2005 1:36 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Destructor for S4 objects?


Hi,

To write a "constructor" for an S4 object, you make an initialize method
which will be called by new. But how would I make a "destructor" method
to
be called when the S4 object is garbage collected? I'm looking at
reg.finalizer, but I'm not sure how to make that work for an S4 object.

I want to write a destructor because my S4 object's initialize method
allocates some resources. I would like the destructor to free them.

I guess I could make one of the slots in my object an environment that
holds
the handles for those resources and register that environment with the
finalizer. Then the finalizer function would release those resources. Is
that the way to do it or is there another simpler way?

Thanks for any help and Happy New Year!

Adam Lyon

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list