Program crash: GC destroys an object unexpectedly

jfondren julian.fondren at gmail.com
Thu Sep 23 14:31:34 UTC 2021


On Thursday, 23 September 2021 at 14:23:40 UTC, eugene wrote:
> On Thursday, 23 September 2021 at 14:00:30 UTC, eugene wrote:
>
>> For the moment I am personally quite happy
>
> ```d
> void main(string[] args) {
>
>     import core.memory : GC;
>
>     auto Main = new Main();
>     GC.addRoot(cast(void*)Main);
>     Main.run();
>
>     auto stopper = new Stopper();
>     GC.addRoot(cast(void*)stopper);
>     stopper.run();
> ```
>
> Fine, works!

Nice. I thought of GC.addRoot several times but I was distracted 
by the general solution of using object lifetimes with it, so 
that a struct's destructor would call GC.removeRoot. For your 
case just pinning these and forgetting about them is the easiest 
way to do it.


More information about the Digitalmars-d-learn mailing list