Stupid question :)
Stupid question :)
If I load a DLL with LoadLibrary, and then calls a func in the DLL and that func allocates memory with the help of LocalAlloc and then unload the DLL with FreeLibrary, the memory that was allocated by the DLL will be freed by the system? :)
If I load a DLL with LoadLibrary, and then calls a func in the DLL and that func allocates memory with the help of LocalAlloc and then unload the DLL with FreeLibrary, the memory that was allocated by the DLL will be freed by the system? :)
if I understand you correct:
ReplyDelete- load dll
- call func in dll(let's call it func_x)
- func_x calls a function within main app that allocates memory(let's call it main_mem_alloc)
- unload dll
-------------------
main_mem_alloc will be leaked, but when app closes, the memory goes back to the OS
Ok and thank You, I thought that... :(
ReplyDeletebut, if you provide a "mem alloc" to the dll, then why not a "mem free" as well? in this way, you can be safe.
ReplyDeleteapache, for example, provides methods for allocating and freeing memory for the loaded modules via api...
The situation is the following:
ReplyDelete- load dll
- call func in dll(let's call it func_x)
- func_x calls WinAPI.LocalAlloc
- unload dll
sorry, not familiar with it...
ReplyDeleteDoes the DLL export a function to free the memory it allocated? Any non broken one will.
ReplyDeleteNo, it allocs the mem in DllMain... :(
ReplyDelete:-( ...of course, the original developer of the dll probably never saw a problem because "who does dynamic linking, right?"
ReplyDeleteCan you patch the LocalAlloc/LocalFree call in the dll to call your pass-through wrapper. Then having seen the values allocated you can maintain a list of items to free once the DLL is unloaded?
ReplyDeleteIf there are many items allocated using LocalAlloc and only some are freed things it may be trick to achieve a clean backing out of all the allocated memory in the right order...
Taking a step back: why does this matter so much?
Is it a large amount - do you do it many times? Is memory constrained?
The problem is that the DllMain is called whenever a thread is started/ended (DLL_THREAD_ATTACH, DLL_THREAD_DETACH)
ReplyDelete[ src: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx ]
We use a lot of thread in our program so this can "eat" memory very fast... :(
Ah: you may be lucky - can you call DisableThreadLibraryCalls
ReplyDelete?
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682579%28v=vs.85%29.aspx
The idea being that the notifications DLL_THREAD_ATTACH, DLL_THREAD_DETACH will stop..
Alternatively, you may need to hook the memory management functions to - ahem - "assist" the dll.
Working code is available in the Jcl to do this, BTW..
Patrick Martin Thank You, I didn't knew this func, checking :)
ReplyDeleteFunc hook already done :)
[ btw. it's a dll from a big software company... ]
Good luck. You may need to be on the lookout for any intended side effects due upsetting the thread notifications...
ReplyDeleteOk, we made contact with the developer company, now waiting for some useful response :)
ReplyDeleteThank You for help! :)