Destructor semantics

Jonathan M Davis jmdavisprog at gmail.com
Wed Aug 11 13:40:40 PDT 2010


On Wednesday, August 11, 2010 13:14:56 Don wrote:
> As far as I can tell, the use cases for finalizers are very limited. To
> me, destructors don't make any sense unless they are deterministic.
> For example, I think having a File class is probably a bug. You may be
> on a system which has no limit on the number of file handles, but
> generally, if you need resource management, you need a guarantee that
> the destructor will be called.
> 
> If a file handle is stored as part of a class, with a destructor that
> closes the file, the file might never get closed. So I don't think it's
> unreasonable to say that a struct with a destructor cannot be a member
> of a class.
> 
> Personally I think destructors should be restricted to structs and scope
> classes. I suspect that some form of registration with the gc could do
> the job of finalizers.
> 
> Destructor <=> deterministic destruction.

It would certainly make destructors more straightforward if they were guaranteed 
to be deterministic. And making it so that they're deterministic in some cases 
and not in others seems like it would make them bug-prone, since doing something 
with a determinstic destructor could be perfectly valid and acceptable while it 
could be a problem in a nondeterministic one.

- Jonathan M Davis


More information about the Digitalmars-d mailing list