Simple memory allocation in shared library leads to crash

Guillaume Piolat first.last at gmail.com
Wed Jan 27 11:14:08 UTC 2021


On Monday, 25 January 2021 at 15:32:40 UTC, Vitalii wrote:
> Can anybody explain why this code crash (memory allocation in 
> shared library)? Try to run test_dll_exe.exe several times:
> ----
> // test_dll.d
> import std.stdio;
> import core.sys.windows.windows;
> import core.sys.windows.dll;
>
> mixin SimpleDllMain;
>
> extern(C) {
>
> export void TestFun() {
>     double[int] T;
>     foreach (i; 0..10_000_000) {
>         writefln("i:%d",i);
>         T[i] = i*1.0;
>     }
> }
>
>
> Any ideas? How to allocate memory in shared library properly?

You did not initialize the D runtime in your shared library.
   - call Runtime.initialize()/Runtime.deinitialize() with this 
pragma: 
https://wiki.dlang.org/LDC-specific_language_changes#LDC_global_crt_ctor_and_LDC_global_crt_dtor (or have separate callbacks for this)
   - attach incoming threads, and detach exiting threads as they 
enter/exit your callback





More information about the Digitalmars-d mailing list