digitalmars.D.learn - Dll crash in simplest case
Hello everyone! I want to create shared library that buffer some
data and do some calculations, another program will use it. I
wonder how this simplest code lead to crash (freeze) of dll:
---
// test_dll.d
import std.stdio;
import std.file;
import core.runtime;
import core.sys.windows.windows;
import std.exception;
version (test_dll) {
extern(C) {
export void TestFun() {
double[int] T;
foreach (i; 0..10000000) {
writefln("i:%d",i);
T[i] = i*1.0;
}
}
} // extern(C)
} // version (test_dll)
version (test_exe) {
void main(string[] args) {
if (!"test_dll.dll".exists) {
writeln("test_dll.dll not exists");
}
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();
} // main
} // version (stec_app)
---
Code compiled with options (version of dmd - 2.095.0, Windows 7
SP1, Intel Core i7 4790):
dmd -m64 -version=test_dll -shared -oftest_dll.dll -L/DLL
test_dll.d dll.d
dmd -m64 -version=test_exe test_dll.d
Here dll.d - usual wrapper for DllMain
(https://wiki.dlang.org/Win32_DLLs_in_D).
Any ideas? How to allocate memory in shared library properly?
Jan 24 2021
On Monday, 25 January 2021 at 07:58:01 UTC, Vitalii wrote:Hello everyone! I want to create shared library that buffer some data and do some calculations, another program will use it. I wonder how this simplest code lead to crash (freeze) of dll:Not tested your code but you have to use import core.sys.windows.dll; mixin SimpleDllMain; Windows ist expecting a DllMain routine and runtime needs to attach the DLL thread.
Jan 25 2021
On Monday, 25 January 2021 at 10:26:20 UTC, frame wrote:On Monday, 25 January 2021 at 07:58:01 UTC, Vitalii wrote:Yes. I'm doing it whet add dll.d (https://wiki.dlang.org/Win32_DLLs_in_D) in compile line, it contents: --- import core.sys.windows.windows; import core.sys.windows.dll; __gshared HINSTANCE g_hInst; extern (Windows) BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved) { switch (ulReason) { case DLL_PROCESS_ATTACH: g_hInst = hInstance; dll_process_attach( hInstance, true ); break; case DLL_PROCESS_DETACH: dll_process_detach( hInstance, true ); break; case DLL_THREAD_ATTACH: dll_thread_attach( true, true ); break; case DLL_THREAD_DETACH: dll_thread_detach( true, true ); break; default: } return true; } --- Dll is starting ok. But after freeze after printing number around 64000 (~2^16). When I change assoc.array to simple array of double[] it freeze after number around 520000 (~2^19), int[] -- 1.000.000. So I conclude than dll freeze after exceed some memory limit about 4 Mb. But I still can't see what I'm doing wrong. Any ideas?Hello everyone! I want to create shared library that buffer some data and do some calculations, another program will use it. I wonder how this simplest code lead to crash (freeze) of dll:Not tested your code but you have to use import core.sys.windows.dll; mixin SimpleDllMain; Windows ist expecting a DllMain routine and runtime needs to attach the DLL thread.
Jan 25 2021
On Monday, 25 January 2021 at 11:30:45 UTC, Vitalii wrote:--- Dll is starting ok. But after freeze after printing number around 64000 (~2^16). When I change assoc.array to simple array of double[] it freeze after number around 520000 (~2^19), int[] -- 1.000.000. So I conclude than dll freeze after exceed some memory limit about 4 Mb. But I still can't see what I'm doing wrong. Any ideas?I'm not an expert but it shouldn't freeze at all. Crashing maybe, but not freeze. Sounds like a problem with the compiler and OS support. A memory limit makes not sense here.
Jan 25 2021
On Monday, 25 January 2021 at 11:30:45 UTC, Vitalii wrote:On Monday, 25 January 2021 at 10:26:20 UTC, frame wrote:try replacing this with mixin SimpleDllMain (it initializes runtime for you IIRC). Runtime.LoadLibrary() is expected to call rt_init too, but Windows support for shared libs is too far from good.[...]Yes. I'm doing it whet add dll.d (https://wiki.dlang.org/Win32_DLLs_in_D) in compile line, it contents: --- import core.sys.windows.windows; import core.sys.windows.dll; __gshared HINSTANCE g_hInst; extern (Windows) BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved) { switch (ulReason) { case DLL_PROCESS_ATTACH: g_hInst = hInstance; dll_process_attach( hInstance, true ); break; case DLL_PROCESS_DETACH: dll_process_detach( hInstance, true ); break; case DLL_THREAD_ATTACH: dll_thread_attach( true, true ); break; case DLL_THREAD_DETACH: dll_thread_detach( true, true ); break; default: } return true; } ---
Jan 25 2021









frame <frame86 live.com> 