How to call destructor before free without dropping @nogc?

Ferhat Kurtulmuş aferust at gmail.com
Thu Aug 19 21:38:36 UTC 2021


On Thursday, 19 August 2021 at 15:38:19 UTC, evilrat wrote:
> On Thursday, 19 August 2021 at 15:12:03 UTC, Ferhat Kurtulmuş

Btw, based on 
https://github.com/dlang/druntime/blob/master/src/object.d#L4209:

import core.lifetime;
import core.stdc.stdio;
import core.stdc.stdlib;

extern (C) void rt_finalize(void *data, bool det=true) @nogc 
nothrow; // cheap hack here
alias destroy = rt_finalize;

     class SomeClass
     {
         int a = 42;

         this() @nogc { }
         ~this() @nogc {printf("nogc\n");}
         this(int val) @nogc { a = val; }
     }



     @nogc void main()
     {
     	SomeClass dynAlloc = cast(SomeClass) 
malloc(__traits(classInstanceSize, SomeClass));
     	dynAlloc = emplace!SomeClass(dynAlloc, 123);
     	printf("dynamic %d\n", dynAlloc.a); // 123
         //rt_finalize(cast(void*)dynAlloc);
         destroy(cast(void*)dynAlloc); // cast needed :/ dunno 
consequences though
     }


More information about the Digitalmars-d-learn mailing list