In this specific case, you can force a collection at the end of main()

void main() {
        auto b = new B;
        a = new A;

        import core.memory;
        GC.collect();
}

The gc runs automatically at the end of the program, but at that point, both b and a are gone, so the gc just gets to it as soon as it can. Manually calling collect after you're done with b but before the program ends (so a is still in use) it should do it in order.

But, generally, if you want to rely on destructors at all, you have to use structs, or at least destroy(). They might never run with classes and the order is undefined - it all depends on when and if the GC ever actually grabs it.

Thought a struct at module scope might never run its destructor at all...

Reply via email to