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? :)

Comments

  1. if I understand you correct:
    - 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

    ReplyDelete
  2. Ok and thank You, I thought that... :(

    ReplyDelete
  3. but, if you provide a "mem alloc" to the dll, then why not a "mem free" as well? in this way, you can be safe.

    apache, for example, provides methods for allocating and freeing memory for the loaded modules via api...

    ReplyDelete
  4. The situation is the following:
    - load dll
    - call func in dll(let's call it func_x)
    - func_x calls WinAPI.LocalAlloc
    - unload dll

    ReplyDelete
  5. Does the DLL export a function to free the memory it allocated? Any non broken one will.

    ReplyDelete
  6. No, it allocs the mem in DllMain... :(

    ReplyDelete
  7. :-( ...of course, the original developer of the dll probably never saw a problem because "who does dynamic linking, right?"

    ReplyDelete
  8. Can 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?
    If 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?

    ReplyDelete
  9. The problem is that the DllMain is called whenever a thread is started/ended (DLL_THREAD_ATTACH, DLL_THREAD_DETACH)
    [ 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... :(

    ReplyDelete
  10. Ah: you may be lucky - can you call DisableThreadLibraryCalls
    ?

    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..

    ReplyDelete
  11. Patrick Martin Thank You, I didn't knew this func, checking :)

    Func hook already done :)

    [ btw. it's a dll from a big software company... ]

    ReplyDelete
  12. Good luck. You may need to be on the lookout for any intended side effects due upsetting the thread notifications...

    ReplyDelete
  13. Ok, we made contact with the developer company, now waiting for some useful response :)

    Thank You for help! :)

    ReplyDelete

Post a Comment