What should happen here?

Walter Bright newshound2 at digitalmars.com
Sat Sep 25 02:15:15 UTC 2021


On 9/21/2021 5:02 AM, Steven Schveighoffer wrote:
> I just thought of a possible easy and effective way to ensure the thing isn't 
> collected early:
> 
> ```d
> struct Pin(T)
> {
>     T t;
>     @nogc nothrow pure @safe ~this() {}
>     alias t this;
> }
> 
> ...
> // usage
> auto c = Pin!C(new C); // now it needs to be held until the scope ends
> ```

Use addRoot()/removeRoot() to do this in a documented and supported fashion.

https://dlang.org/phobos/core_memory.html#addRoot


More information about the Digitalmars-d mailing list