Segfault when adding a static destructor in druntime/src/rt/sections_elf_shared.d

Steven Schveighoffer schveiguy at
Thu Jan 10 15:04:25 UTC 2019

On 1/8/19 7:54 AM, RazvanN wrote:
> Hi all,
> I am working on issue 14650 [1] and I would like to implement a solution 
> where static destructors are destroying global variables. However, I 
> have the following problem in druntime/src/rt/sections_elf_shared:
> struct ThreadDSO
> {
>      DSO* _pdso;
>      static if (_pdso.sizeof == 8) uint _refCnt, _addCnt;
>      else static if (_pdso.sizeof == 4) ushort _refCnt, _addCnt;
>      else static assert(0, "unimplemented");
>      void[] _tlsRange;
>      alias _pdso this;
>      // update the _tlsRange for the executing thread
>      void updateTLSRange() nothrow @nogc
>      {
>          _tlsRange = _pdso.tlsRange();
>      }
> }
> Array!(ThreadDSO) _loadedDSOs;
> For this code, I would have to create the following static destructor:
> static ~this() { _loadedDSOs.__dtor(); }
> Because Array defines a destructor which sets its length to 0.
> However this code leads to segfault when compiling any program with the 
> runtime (betterC code compiles successfully). In my attempt to debug it, 
> I dropped my patch and added the above mentioned static destructor 
> manually in druntime which lead to the same effect. Interestingly, 
> _loadedDSOs.__dtor runs successfully, the segmentation fault comes from 
> somewhere higher in the call path (outside of the _d_run_main function 
> (in rt/dmain2.d)). I'm thinking that the static destructor somehow 
> screws up the object which is later referenced after the main program 
> finished executing. Does someone well versed in druntime has any ideas 
> what's happening?

That is a thread-local static destructor. Are any shared static 
destructors accessing the array?

You might be able to determine this by printf debugging between calling 
unshared and shared destructors.


More information about the Digitalmars-d-learn mailing list