Destructors and Deterministic Memory Management
Daniel Keep
daniel.keep.lists at gmail.com
Sun May 3 16:53:42 PDT 2009
Georg Wrede wrote:
> dsimcha wrote:
>> Two closely related topics here:
>>
>> 1. It is often nice to be able to create a single object that works
>> with both
>> GC and deterministic memory management. The idea is that, if delete
>> is called
>> manually, all sub-objects would be freed deterministically, but the
>> object
>> could still safely be GC'd. Since the destructor called by the GC can't
>> reference sub-objects, would it be feasible to have two destructors
>> for each
>> class, one that is called when delete is invoked manually and another
>> that is
>> called by the GC?
>
> This one you already can do. Just write a myDestructor() that does
> whatever you need, and then call it when necessary.
Actually, a pattern I stole from C# was dispose. I have a module lying
around somewhere that defines a Disposable interface with a void
dispose(); method. There's also a mixin that implements much of the
logic and boilerplate for you.
It also defines void dispose(T)(T) and void destroy(T)(ref T); destroy
works like delete except that it will call dispose on the passed value
if it can find it. Then I just use destroy everywhere instead of
delete, unless it's for a scoped instance in which case I use dispose.
-- Daniel
More information about the Digitalmars-d
mailing list