From simon.urbanek at r-project.org  Tue Jul 21 19:26:36 2009
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Tue, 21 Jul 2009 13:26:36 -0400
Subject: [Rd] destructor for S4 class objects in analogy to C++
In-Reply-To: <1a3e78310907211000g50935bb7l867e4a0c97fe12a@mail.gmail.com>
References: <1a3e78310907211000g50935bb7l867e4a0c97fe12a@mail.gmail.com>
Message-ID: <811E2752-C23C-465B-A3E9-711FC1C6E482@r-project.org>

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



[Rd] destructor for S4 class objects in analogy to C++
Simon Urbanek