David Scherrer
2009-Jul-21 17:00 UTC
[Rd] destructor for S4 class objects in analogy to C++
Hi all, I'm wondering if there is a way to define a destructor function (to free memory) for S4 class objects in analogy to C++? rm() combined with gc() does not seem to be a good idea (Chambers, 2008). So could it be done on the C/C++ level or is it even already available in the "internals"? Many thanks, David [[alternative HTML version deleted]]
Whit Armstrong
2009-Jul-21 17:22 UTC
[Rd] destructor for S4 class objects in analogy to C++
if you are using external pointers, then R allows you to set a finalizer function to be called on the pointer. http://cran.r-project.org/doc/manuals/R-exts.html#External-pointers-and-weak-references see examples in RODBC package. and here too: see the "connFinalizer" function http://github.com/armstrtw/unifieddbi/blob/3b21da83f96de231fb14a1adaf9150fa1bc6a277/src/interface.cpp -Whit On Tue, Jul 21, 2009 at 1:00 PM, David Scherrer<dasmailinglists at gmail.com> wrote:> Hi all, > > I'm wondering if there is a way to define a destructor function (to free > memory) for S4 class objects in analogy to C++? > rm() combined with gc() does not seem to be a good idea (Chambers, 2008). > So could it be done on the C/C++ level or is it even already available in > the "internals"? > > Many thanks, > David > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Simon Urbanek
2009-Jul-21 17:26 UTC
[Rd] destructor for S4 class objects in analogy to C++
David, On Jul 21, 2009, at 13:00 , David Scherrer wrote:> I'm wondering if there is a way to define a destructor function (to > free memory) for S4 class objects in analogy to C++?The analogy doesn't work, because in C++ you have pass-by-reference semantics, but in R you have pass-by-value. Hence in R there is no "one" object that you could finalize - there may be dozens of copies of your S4 object, so which one would you finalize? However, you can register a finalizer for reference-semantics objects (if that's what you really have), see ?reg.finalizer (and on C level R_Register*Finalizer* functions). Cheers, Simon> rm() combined with gc() does not seem to be a good idea (Chambers, > 2008). > So could it be done on the C/C++ level or is it even already > available in > the "internals"? > > Many thanks, > David > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >