dlang bug - accessing module variable from method segfaults only when using module reference directly

Mike Parker aldacron at gmail.com
Fri Jul 1 13:20:15 UTC 2022


On Friday, 1 July 2022 at 13:01:30 UTC, Chris Katko wrote:
> Forgot the last line. That's important because world MUST exist 
> by time elf is called... because world... created and called 
> elf.
>
> So it's not a memory issue, but some sort of linkage issue.

world is null because the constructor didn't complete. The 
segfault happens inside its constructor.

And that also looks like the source of your original segfault. 
You've got a circular reference going on in the constructors. In 
other words, you're constructing a global world instance, which 
in turn constructs an elf instance, which in turn accesses the 
global world reference whose constructor hasn't yet completed, so 
the global world reference is still null.

If the objects world is constructing absolutely need to access 
it, then you could:

1. Initialize world with a do-nothing destructor, then call a 
`setup` method on it to do what its constructor currently is 
doing;
2. Pass `this` along to all the constructors that need it from 
inside the world constructor.


More information about the Digitalmars-d-learn mailing list