Destructor called while object is still alive

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Fri Oct 23 16:14:20 UTC 2020


On Friday, 23 October 2020 at 16:00:58 UTC, Steven Schveighoffer 
wrote:
> But you don't get RAII with classes or pointers. Only with 
> structs.

But that is only a consequence of the optimizer not being written 
with a GC in mind. That is the point. Isn't D structs the same as 
C++ PODs? I thought C++ classes with virtuals/RTTI is supposed to 
map to D classes? Then RAII has to work.

> I don't think so. A class reference itself doesn't have a 
> destructor. The compiler is free to elide storage just like it 
> was a pointer. One cannot expect any specific behavior from the 
> GC in this case, as the correctness of the program doesn't 
> depend on the object staying in place for the duration of the 
> function. Here there is just a disconnect in how the OP expects 
> the compiler to implement the machine code.

That would be very surprising semantics as you expect a pointer 
to be LIVE throughout the scope in which it was instantiated. If 
it is LIVE then it cannot be collected by the GC.

This all boils down to the optimizer being GC ignorant. I 
understand that it is too much work to make the optimizer take 
that into consideration, but it most certainly goes against the 
expected norm for the semantics of BLOCKS that LIVE pointers are 
being collected.



More information about the Digitalmars-d mailing list