new should lower to a template function call

Per Nordlöw per.nordlow at gmail.com
Thu Jul 23 18:26:32 UTC 2020


On Thursday, 23 July 2020 at 12:16:55 UTC, Per Nordlöw wrote:
> I actually see 3 (instead of 2) kinds of allocations here:
>
> 1. GC: destruction during `GC.collect()`
> 2. C/C++-style heap: (too large to fit on stack) scoped 
> destruction (could be inferred?)

The passing of the unittest in the following module verifies that 
`scope`-qualified class variables are destructed when they go out 
of scope even when the GC is disabled. Nice. If the scope 
qualfifier is removed from `x` then the unittest fails as 
expected.


/** Test how destruction of scoped classes is handled.
  *
  * See_Also:
  */
module scoped_class_dtor;

bool g_dtor_called = false;

class C
{
@safe nothrow @nogc:
     this(int x) { this.x = x; }
     ~this() { g_dtor_called = true; }
     int x;
}

void scopedC() @safe nothrow
{
     scope x = new C(42);
}

unittest
{
     import core.memory : GC;
     GC.disable();
     scopedC();
     assert(g_dtor_called);
     GC.enable();
}


Does this mean that the allocation of `scope`d classes is done on 
a thread-local heap separate from the GC-heap? I would be nice to 
get a reference to the places in dmd and/or druntime were this 
logic is defined.


More information about the Digitalmars-d mailing list