digitalmars.D - Simple memory allocation in shared library leads to crash
- Vitalii (40/40) Jan 25 2021 Can anybody explain why this code crash (memory allocation in
- Bastiaan Veelo (4/6) Jan 25 2021 I ran it once, successfully. Maybe a hardware issue? I will give
- Bastiaan Veelo (4/10) Jan 26 2021 Tried it six times now, works fine every time. DMD32 D Compiler
- Guillaume Piolat (7/24) Jan 27 2021 You did not initialize the D runtime in your shared library.
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?
Jan 25 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:I ran it once, successfully. Maybe a hardware issue? I will give it a few more tries later. -- Bastiaan.
Jan 25 2021
On Monday, 25 January 2021 at 22:17:01 UTC, Bastiaan Veelo wrote:On Monday, 25 January 2021 at 15:32:40 UTC, Vitalii wrote:Tried it six times now, works fine every time. DMD32 D Compiler v2.094.1-dirty. -- Bastiaan.Can anybody explain why this code crash (memory allocation in shared library)? Try to run test_dll_exe.exe several times:I ran it once, successfully. Maybe a hardware issue? I will give it a few more tries later. -- Bastiaan.
Jan 26 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
Jan 27 2021