Hi All,
I have an application that's running all the time that is calling and releasing various DLLs. In some cases, I cannot delete some of the memories allocated by the DLLs for some technical reasons. My question is, therefore, if there are some undeleted memory that are heap
allocated by a DLL, are these released automatically when the DLL is detached from the calling process or do they remain in the heap as long as the application calling the DLL is running? I have the same question
regarding global variables declared and used in a DLL.
I have a confusion in this area and I appreciate any
helpful reply.
Thank you in advance!
Best regards,
Metty
Memory allocated by a DLLMemory management
Page 1 of 1
8 Replies - 2894 Views - Last Post: 15 January 2008 - 01:54 AM
Replies To: Memory allocated by a DLL
#2
Re: Memory allocated by a DLL
Posted 06 January 2008 - 12:41 PM
I believe once the DLL has been called and then used the application will release the memory space automatically.
#3
Re: Memory allocated by a DLL
Posted 06 January 2008 - 03:47 PM
Detaching the DLL won't release heap allocated resources, it must be done by the application (since probably it is using those "objects").
#4
Re: Memory allocated by a DLL
Posted 10 January 2008 - 01:50 AM
The FreeLibrary() API Function is to be used to release memory that is being used by a specific DLL File.
At times, programmers forget to add a simple Function call which does result in unnecessary usage of Memory.
That depends.
If you're accessing the DLL file using one of .NET Framework's Classes, you don't need to worry about Memory Management. The Class takes care of that on its own.
But if you are using direct API Function Calls, you will need to free memory yourself.
Global Variables lose scope as soon as the DLL file is detached.
Here's some info from MSDN:
Hence, a missing FreeLibrary() call would prevent the DLL from being unmapped even though it is not in use.
At times, programmers forget to add a simple Function call which does result in unnecessary usage of Memory.
Quote
My question is, therefore, if there are some undeleted memory that are heap
allocated by a DLL, are these released automatically when the DLL is detached from the calling process or do they remain in the heap as long as the application calling the DLL is running?
allocated by a DLL, are these released automatically when the DLL is detached from the calling process or do they remain in the heap as long as the application calling the DLL is running?
That depends.
If you're accessing the DLL file using one of .NET Framework's Classes, you don't need to worry about Memory Management. The Class takes care of that on its own.
But if you are using direct API Function Calls, you will need to free memory yourself.
Quote
I have the same question
regarding global variables declared and used in a DLL.
regarding global variables declared and used in a DLL.
Global Variables lose scope as soon as the DLL file is detached.
Here's some info from MSDN:
Quote
Each process maintains a reference count for each loaded library module. This reference count is incremented each time LoadLibrary is called and is decremented each time FreeLibrary is called. A DLL module loaded at process initialization due to load-time dynamic linking has a reference count of one. This count is incremented if the same module is loaded by a call to LoadLibrary.
Before unmapping a library module, the system enables the DLL to detach from the process by calling the DLL's DllMain function, if it has one, with the DLL_PROCESS_DETACH value. Doing so gives the DLL an opportunity to clean up resources allocated on behalf of the current process. After the entry-point function returns, the library module is removed from the address space of the current process.
Before unmapping a library module, the system enables the DLL to detach from the process by calling the DLL's DllMain function, if it has one, with the DLL_PROCESS_DETACH value. Doing so gives the DLL an opportunity to clean up resources allocated on behalf of the current process. After the entry-point function returns, the library module is removed from the address space of the current process.
Hence, a missing FreeLibrary() call would prevent the DLL from being unmapped even though it is not in use.
#5
Re: Memory allocated by a DLL
Posted 10 January 2008 - 04:20 AM
Excellent explanation! It was on my mind for a while to come back and go into more depth, but you were quicker!
#6
Re: Memory allocated by a DLL
Posted 10 January 2008 - 04:04 PM
Thank you very much for the explanation. However, I am not sure if I got the point you wanted to convey when you say:
>Hence, a missing FreeLibrary() call would prevent the DLL from being >unmapped even though it is not in use.
Would you be so kind to explain a little bit more in detail. I hope this will solve to solve my problem.
Best regards,
Metty
>Hence, a missing FreeLibrary() call would prevent the DLL from being >unmapped even though it is not in use.
Would you be so kind to explain a little bit more in detail. I hope this will solve to solve my problem.
Best regards,
Metty
#7
Re: Memory allocated by a DLL
Posted 11 January 2008 - 11:59 AM
Before any function from a DLL can be called, the DLL File has to be loaded in memory.
This is done using the LoadLibrary() function.
In the Win32 API, the programmer is responsible for managing memory.
Hence, he/she must also free the memory used up by the DLL File by calling the FreeLibrary() function.
If this is not done, the DLL file remains in memory until the program ends.
This is done using the LoadLibrary() function.
In the Win32 API, the programmer is responsible for managing memory.
Hence, he/she must also free the memory used up by the DLL File by calling the FreeLibrary() function.
If this is not done, the DLL file remains in memory until the program ends.
#8
Re: Memory allocated by a DLL
Posted 12 January 2008 - 11:21 AM
born2c0de, on 11 Jan, 2008 - 11:59 AM, said:
Before any function from a DLL can be called, the DLL File has to be loaded in memory.
This is done using the LoadLibrary() function.
In the Win32 API, the programmer is responsible for managing memory.
Hence, he/she must also free the memory used up by the DLL File by calling the FreeLibrary() function.
If this is not done, the DLL file remains in memory until the program ends.
This is done using the LoadLibrary() function.
In the Win32 API, the programmer is responsible for managing memory.
Hence, he/she must also free the memory used up by the DLL File by calling the FreeLibrary() function.
If this is not done, the DLL file remains in memory until the program ends.
Thank you again.
But loading and unloading can also be done by an automatic call to the DllMain function. I don't use LoadLibrary() to load the library so I must not use FreeLibrary(). Am I wrong?
So, my question was when a DLL is detached as a result of a call to the DllMain function, what happens to the heap-allocated memory allocated by the DLL? Are they freed or not? My understanding from the discussion so far is that heap-allocated memory should be deallocated by the programmer. Am I right? I just want to be sure on this point because if the memory is not released, I may have to find out deallocating the memory allocated in the DLL which I couldn't deallocate for a reason so far.
Thank you again.
Metty
#9
Re: Memory allocated by a DLL
Posted 15 January 2008 - 01:54 AM
Quote
But loading and unloading can also be done by an automatic call to the DllMain function
The DllMain() Function is called automatically even if you use LoadLibrary().
How are you calling the DLL function? Are you using GetProcAddress()?
Quote
My understanding from the discussion so far is that heap-allocated memory should be deallocated by the programmer. Am I right?
Yes.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|