Destructor called while object is still alive

Boris Carvajal boris2.9 at gmail.com
Fri Oct 23 12:53:52 UTC 2020


On Friday, 23 October 2020 at 08:32:45 UTC, Ola Fosheim Grøstad 
wrote:
> Does not work with LDC -O3:
>
> void main() {
>     scope foo = Bar.create();
>     GC.collect();
>     writefln("Collection done");
>     exit(0);
> }
>
> Output:
>
> Warning: destructor called (onlineapp.Foo) on 55A899C1C570
> Collection done

Wow I couldn't believe 'scope' didn't work because it's used a 
lot for this same purpose.

But after checking DMD sources I realized that 'scope' works only 
if it precedes a new expression initializer (scope var = new 
ClassName...) which makes sense but it's really confusing.

That means in OP example 'scope' does nothing, it's just like 
'auto' and the class is heap allocated.


More information about the Digitalmars-d mailing list