D DLL crashes if not run on the main thread

Guillaume Piolat first.name at gmail.com
Tue Sep 5 23:11:34 UTC 2023


On Tuesday, 5 September 2023 at 22:45:28 UTC, raven09 wrote:
>
> I *assume* that this has something to do with D's GC? But I 
> tried calling GC.disable() and nothing changed. Any help or 
> insight would be appreciated.
> Thanks in advance

If you want to have a D DLL called from elsewhere, and don't 
control which threads call your dynlib:
- first thread that comes (or the DLL load itself) should 
initialize the runtime. You can use pragma(crt_destructor) to 
deinitialize the D runtime.
- threads that come through a callback should in general register 
to the D runtime, and unregister on exit. They will not hold 
roots while they are back in C land, and you must unregister them 
on exit else the GC will try to pause possibly dead threads when 
it collects.
You can leave some threads unregistered, but then I'm not sure 
for TLS and they certainly cannot hold GC roots.

You can also leave the whole D runtime disabled, but that is 
annoying (no GC, amongst other things).


More information about the Digitalmars-d-learn mailing list