Should destructors be able to tell who called them?

Michel Fortin michel.fortin at michelf.com
Tue Aug 10 11:38:13 PDT 2010


On 2010-08-10 14:25:48 -0400, "Steven Schveighoffer" 
<schveiguy at yahoo.com> said:

> ~this(bool deterministic)
> {
>     if(deterministic) file.close();
> }
> 
> or like this:
> 
> ~this()
> {
> }
> 
> Scope classes (if allowed to exist) or calls to clear will set  
> deterministic to true.  The GC will set it to false.
> 
> This would make destructors a lot more useful.  Thoughts?

In Objective-C we have the dealloc method (deterministic) and the 
finalize method (non-deterministic). The former is used in a 
reference-counted environment while the second is used when memory is 
managed by a GC.

One issue with having one destructor with a deterministic argument (as 
opposed to having two separate destructors) is that non-deterministic 
destructors should probably not be allowed in SafeD because you can 
then get access to dangling pointers and you can even pass them around 
to make them live long after the collection has terminated.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list