On Sat, 8 Jan 2005, McGehee, Robert wrote:

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).

rm() does not remove the object, just mark it as no longer in use by removing the symbol. If you want to release the storage (to R, at least), you need to call gc().


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

There are ways around that, as finalizers can be told to run then. See the examples in the RODBC package.

OTOH, .Last.lib is run only when a package is explicitly detached, and not at the end of the session.

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.

Quitting does not necssarily happen through q() (EOF is another way): but you can use .Last.


--
Brian D. Ripley,                  [EMAIL PROTECTED]
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

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

Reply via email to