scope(exit) and destructor prioity

Moritz Maxeiner moritz at ucworks.org
Mon Sep 18 21:23:17 UTC 2017


On Monday, 18 September 2017 at 20:55:21 UTC, Sasszem wrote:
>
> If I write "auto a = new De()", then it calls the scope first, 
> no matter where I place it.

Because with `new`
a) your struct object is located on the heap (and referred to by 
pointer - `De*`) instead of the stack (which means no destructors 
for it are called at function scope end), and
b) the lifetime of your struct object is determined by D's 
garbage collector, which may or may not eventually collect it, 
finalizing it in the process (calling the destructor, as D 
doesn't separate finalizers and destructors a.t.m.).
In your case, it sounds like the GC collection cycle that (in the 
current implementation) occurs just before druntime shutdown 
collects it.
I highly recommend reading The GC Series on the D blog [1].

[1] https://dlang.org/blog/the-gc-series/


More information about the Digitalmars-d-learn mailing list