digitalmars.D.bugs - [Issue 20248] New: Module constructors in executable called twice, in
- d-bugmail puremagic.com (45/45) Sep 27 2019 https://issues.dlang.org/show_bug.cgi?id=20248
https://issues.dlang.org/show_bug.cgi?id=20248 Issue ID: 20248 Summary: Module constructors in executable called twice, in Product: D Version: D2 Hardware: x86_64 OS: Linux Status: NEW Severity: major Priority: P1 Component: druntime Assignee: nobody puremagic.com Reporter: john.loughran.colvin gmail.com % cat modcon.d import modcon2; void main() { import core.runtime : Runtime; import core.sys.posix.dlfcn; import std.stdio; writeln("HI"); assert(Runtime.loadLibrary("./libmodcon2.so")); } % cat modcon2.d __gshared int a; shared static this() { foo(); } void foo() { import core.stdc.stdio; printf("%p ", &a); printf("%p\n", &foo); } % dmd -shared modcon2.d -of=libmodcon2.so % dmd -defaultlib=libphobos2.so modcon.d modcon2.d % ./modcon 0x7f920f3a2140 0x7f920f39f930 HI 0x7f920f3a2140 0x7f920f39f930 As you can see from the printed pointers, the module constructor from the executable is called twice - once on process start and once on loading the shared library - and the module constructor from the shared library is never called. Module constructors are normally designed to be called only once, so this causes some havok --
Sep 27 2019