Leak-detection of references to scoped class instances

Per Nordlöw per.nordlow at gmail.com
Mon Nov 25 22:51:55 UTC 2019


On Monday, 25 November 2019 at 13:09:44 UTC, mipri wrote:
> gdc assembly. ldc eliminates the object entirely.

DIP-1000 kicks comes to the rescue here aswell:


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

C leakClass() @safe pure nothrow
{
     scope x = new C(42);
     return x;                   // Error: scope variable `x` may 
not be returned
}

@safe pure nothrow unittest
{
     auto x = leakClass();
}


Brilliant!


More information about the Digitalmars-d-learn mailing list