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

Mike Parker aldacron at gmail.com
Fri Jul 1 13:24:49 UTC 2022


On Friday, 1 July 2022 at 13:20:15 UTC, Mike Parker wrote:
r.
>
> 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.
>

Here's what it looks like in code:

```d
import std.stdio : writeln;

class Foo {
     Bar b;
     this() { b = new Bar; }
     void sayMyName() { writeln("I am Foo."); }
}

class Bar {
     this() { f.sayMyName(); }
}

Foo f;

void main()
{
     f = new Foo;
}
```


More information about the Digitalmars-d-learn mailing list