Simple memory allocation in shared library leads to crash
    Vitalii 
    yvitaliy1980 at yandex.ru
       
    Mon Jan 25 15:32:40 UTC 2021
    
    
  
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;
     }
}
}
----
// test_dll_exe.d
import core.runtime;
import core.sys.windows.windows;
import std.exception;
void main() {
     HMODULE test_dll = cast(HMODULE) 
enforce(Runtime.loadLibrary("test_dll.dll"));
     alias extern(C) void function() Test_type;
     Test_type TestFun = cast(Test_type) 
enforce(GetProcAddress(test_dll, "TestFun"));
     TestFun();
}
----
Problem reproduces with dmd 2.095.0 and ldc2 1.24 on Windows 7 
SP1, Intel i7 4790.
Compile options:
dmd -m64 -shared -oftest_dll.dll -L/DLL test_dll.d dll.d
dmd -m64 test_dll_exe.d
or
ldc2.exe --m64 --shared --of=test_dll.dll -L=/DLL test_dll.d
ldc2.exe --m64 test_dll_exe.d
Any ideas? How to allocate memory in shared library properly?
    
    
More information about the Digitalmars-d
mailing list