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

Mike Parker aldacron at gmail.com
Fri Jul 1 14:05:49 UTC 2022


On Friday, 1 July 2022 at 13:44:20 UTC, Chris Katko wrote:

> It appears module access to a class is broken until the 
> constructor finishes.

No, it has nothing to do with the module. It's the reference 
itself.

Until the constructor returns, the reference through which you're 
constructing the instance is null. It doesn't matter if it's at 
module scope, function scope, or wherever. If the constructor 
fails to complete (segfault, thrown exception, assertion failure, 
etc.), then the reference remains null.

The reference is not the *instance*. It's a pointer to the 
instance. The instance is valid when the constructor is called, 
because the `this` reference has to be valid. Think of it in 
terms of a normal function call:

```D
T newT() {
     T t = allocT();
     t.construct(t);
     return t;
}

T g = newT();
```

If `t.construct` throws or crashes, then `return t` is never 
executed, and `g` is never initialized.



More information about the Digitalmars-d-learn mailing list