01-13-2010 04:18 PM - edited 01-13-2010 04:22 PM
I have written a dll in which I have multiple threads. Threads run forever until uninitialize function is called. I am calling this dll from other CVI application which I am debugging. Sometimes my application exits prematurely(I am still working on it
CVI applications---->calls dll--->dll loads threads--->threads active in dll---> CVI application exits without uninitializing DLL
Is there anyway to know from dll if parent application is still running? I can terminate dll itself from dll if parent application is not active anymore.
Any other idea?
Thanks.
01-14-2010 03:22 AM
You could work with the DLL_PROCESS_DETACH case within the DllMain() function - I think this should still be called by the system even in the event of a calling process crash. Worth a try.
JR
01-14-2010 08:42 AM
DllMain will be called if the process is terminating, but MSDN give a pretty strong warning about doing anything under these circumstances:
When handling DLL_PROCESS_DETACH, a DLL should free resources such as heap memory only if the DLL is being unloaded dynamically (the lpReserved parameter is NULL). If the process is terminating (the lpvReserved parameter is non-NULL), all threads in the process except the current thread either have exited already or have been explicitly terminated by a call to the ExitProcess function, which might leave some process resources such as heaps in an inconsistent state. In this case, it is not safe for the DLL to clean up the resources. Instead, the DLL should allow the operating system to reclaim the memory.
While it's obvious from your question that you already know this, the best solution is to write your calling process in such a manner that you can guarantee the dll is uninitialized properly under all circumstances (assuming your code never crashes - a safe assumption right?? 🙂 )
NickB
National Instruments