Using DMD64 D Compiler v2.071.0 Trivial code is as follow ---- import std.stdio; class B { int n = 0; void foo() { n++; } } B b; void f() { b.foo(); } void main() { f(); } ---- Output is : Program exited with code -11 The code is compiled with default parameters of a freshly created dub project and executed with dub run. I implemented this useless code because I wanted to benchmark f(). The outcome was unexpected.
Classes in D are reference types. By `B b` you define a null reference of B class and trying to call a method of it crashes program. You need to do `b = new B` somewhere.
Not a bug in D, simple programmer error. Pointers and references (not that all class objects are implicitly references, like Java or C#, but unlike C++) are automatically initialized to null exactly so it segfaults consistently to tell you that you made a mistake. To fix it, you need to initialize the object before use. For your case, you might just make B a struct instead of class. Classes' strength is found in interfaces and inheritance and you don't need that here, so struct is probably more appropriate. Structs are not references, so they will never be null.