Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 25, 2021 Simple memory allocation in shared library leads to crash | ||||
---|---|---|---|---|
| ||||
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? |
January 25, 2021 Re: Simple memory allocation in shared library leads to crash | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vitalii | 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.
|
January 26, 2021 Re: Simple memory allocation in shared library leads to crash | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bastiaan Veelo | 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:
>> 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.
Tried it six times now, works fine every time. DMD32 D Compiler v2.094.1-dirty.
-- Bastiaan.
|
January 27, 2021 Re: Simple memory allocation in shared library leads to crash | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vitalii | 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 |
Copyright © 1999-2021 by the D Language Foundation