The GC, destructors, exceptions and memory corruption

Max Samukha maxter at spambox.com
Fri May 13 10:16:03 PDT 2011


On 05/13/2011 04:44 PM, Don wrote:
> Alexander wrote:
>> On 13.05.2011 10:25, Don wrote:
>>
>>> Are you talking about *finalizers* or *destructors* ?
>>
>> Destructors as defined in D spec. There are no finalizers (yet), AFAIK.
>
> The things in classes which the spec calls "destructors" are finalizers,
> not destructors.
>
>>
>>> Throwing from inside a destructor should definitely work (unlike C++).
>>
>> How? Destructor is called by the GC when object is deleted. When and
>> where (which thread) this happens is unknown, it is done outside of
>> regular flow of execution - so who and where can catch this exception?
>> So, like any other uncatched exception,
>> it will terminate the program.
>
> If the GC calls it, it's a finalizer, not a destructor.
>
> Structs have destructors, and it's perfectly OK to throw inside them.

Destructor/finalizer confusion is one of D's weakest spots. GC and 
"delete" (now - "clear") should have never been designed to call the 
same function. Now that there are talks about making GC call destructors 
on structs, structs are at the threat of becoming as broken as classes are.


More information about the Digitalmars-d mailing list