Destructor called while object is still alive

Steven Schveighoffer schveiguy at gmail.com
Fri Oct 23 16:42:45 UTC 2020


On 10/23/20 12:33 PM, Ola Fosheim Grøstad wrote:
> On Friday, 23 October 2020 at 16:27:53 UTC, Kagamin wrote:
>> Your C++ glasses are too thick, the snippet uses reference type, 
>> there's no RAII there.
> 
> "Resource acquisition is initialization" is typically done with 
> reference types. Doesn't matter whether you name them "struct" or 
> "class". So what? It is a completely bogus argument. Since D now has a 
> precise GC you should get reliable destruction if you avoid unions.
> 

There's no RAII for classes or structs allocated on the heap in C++. You 
have to manually manage the lifetime (either by manually freeing it or 
encapsulating it into an RAII structure like shared_ptr). You only get 
RAII for items allocated on the stack.

But the *resource* is likely a reference. The *owner* of the resource is 
the struct that lives on the stack. A class reference is simply a 
pointer, not an RAII-enabled structure.

-Steve


More information about the Digitalmars-d mailing list