Deterministic resource freeing - delete deprecation (again)

Steven Schveighoffer schveiguy at yahoo.com
Wed Apr 27 07:48:08 PDT 2011


On Wed, 27 Apr 2011 10:32:57 -0400, Alexander <aldem+dmars at nk7.net> wrote:

> On 27.04.2011 16:25, Steven Schveighoffer wrote:
>
>>> You mean like "dispose" in Tango. That's called when "delete" or  
>>> "scope" is used.
>>
>> Yes, that is exactly what I was thinking of (couldn't think of the  
>> name!).  It's actually used I think in Java and C# as well.
>
>   Well, not exactly like this in C# - Dispose() is intended to free any  
> unmanaged resources, which are referenced by object, but not the memory  
> allocated to object itself.

dispose is supposed to be called deterministically (i.e. not by the GC),  
so yeah, it's the same thing.

basically, dispose cleans up resources *knowing* that all its references  
are still valid.

The destructor (finalizer) cleans up only resources it knows are not GC  
managed.

So for instance, if you have a Socket class which contains a file  
descriptor, and a SocketStream class that contains a Socket,  
SocketStream.dispose would call Socket.dispose, which would close the file  
descriptor.  But the destructor of SocketStream would not call  
Socket.dispose, since it does not know if the destructor could be called.

The deallocation of object memory is a separate thing (outside dispose or  
the destructor).

-Steve


More information about the Digitalmars-d mailing list