[R] Destructor for S4 objects?

Adam Lyon lyon at fnal.gov
Fri Jan 7 19:35:35 CET 2005


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




More information about the R-help mailing list