Is there a memory leak when unloading from a DLL a leak in the host process?

Consider this case:

dll = LoadDLL() dll->do() ... void do() { char *a = malloc(1024); } ... UnloadDLL(dll); 

At this point, will 1k again be allocated in the malloc () call for the host process? The DLL is statically linked to CRT.

+7
c ++ memory-management dll winapi crt
source share
6 answers

No, you do not leak.

If you mix dll models (static, dynamic), you can get a memory error if you allocate memory in the dll that you produce in another (or free in exe)

This means that the heap created by the statically linked CRT is not the same heap as the other CRT DLL.

If you contacted the dynamic version of CRT, you will have a leak, since the heap will be used among all dynamically linked CRTs. This means that you should always design applications to use dynamic CRTs or ensure that you never control memory across the dll border (i.e. if you allocate memory in the dll, always provide a program to free it in the same DLL)

0
source share
  • The memory used by the process monitored by the OS is applicable to the full process and does not apply to the DLL.

  • Memory is provided to the program in pieces of the OS called heaps.

  • Heap managers (malloc / new, etc.) further separate the pieces and direct them to request the code.

  • Only when a new heap is assigned does the OS detect an increase in memory.

  • When the DLL is statically linked to the C runtime library (CRT), a private copy of CRT with CRT functions that invoke the DLL call is compiled and placed in a binary DLL. Malloc is also involved.

  • This private copy of malloc will be called whenever code present inside a statically linked DLL tries to allocate memory.

  • Therefore, the private heap visible only to this copy of malloc is acquired from the OS by this malloc and allocates the memory requested by the code inside this private heap.

  • When the DLL is unloaded, it unloads its private heap, and this leak goes unnoticed, as the whole heap is returned back to the OS .

  • However, if the DLL is dynamically linked, the memory is allocated by one common version of malloc, global for all code that is linked in common mode.

  • The memory allocated by this global malloc exits the heap, which is also the heap used for all other code that is linked in dynamic aka shared mode, and therefore is shared. Therefore, any leaks from this heap become a leak that affects the entire process.

Edit - Added binding script descriptions.

+5
source share

You can not say. It depends on the implementation of your static and dynamic CRT. This may even depend on the size of the allocation, since there are CRTs that redirect large allocations to the OS, but implement their own heap for small allocations.

The problem with a CRT that is proceeding, of course, is leaking. The problem with CRT, which is not leaking, is that the executable can reasonably expect memory usage, since malloc's memory must remain usable until a free call is called.

+4
source share

From MSDN Potential Errors Passing CRT Objects on DLL Borders

Each copy of the CRT library has a separate and separate state. As such, CRT objects, such as file descriptors, environment variables and locales, are only valid for a CRT copy where these objects are selected or set. When the DLL and its users use different copies of the CRT library, you cannot transfer these CRT objects across the boundary of the DLL and expect them to be correctly picked up on the other side.

In addition, since each CRT copy has its own heap manager, allocating memory in one CRT library and passing the pointer through the DLL to the boundary that another copy of the CRT needs to free up is a potential cause of a heap break.

Hope this helps.

+2
source share

Actually, the marked answer is incorrect. This is directly a leak. Although it is technically possible for each dll to implement its own heap and free it when it is turned off, most of the "runtime" heaps - static or dynamic - are wrappers of the Win32 process API.

If someone has not been particularly careful to ensure that this is not the case, the DLL will leak the distribution to the load, do, unload the cycle.

+2
source share

You can run a test and see if there are any memory leaks. You run a simple test 30 times, allocating 1 MB each time. You should figure it out pretty quickly.

One thing is certain. If you allocated memory in a DLL, you should also free that memory there (in the DLL).

For example, you should have something like this (a simple but intuitive pseudocode):

 dll = DllLoad(); ptr = dll->alloc(); dll->free(ptr); DllUnload(dll); 

This needs to be done because the DLL has a different heap than the original process (which loads the dll).

+1
source share

All Articles