Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,005 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,350 people online right now. Registration is fast and FREE... Join Now!




Memory allocated by a DLL

 
Reply to this topicStart new topic

Memory allocated by a DLL, Memory management

metty
6 Jan, 2008 - 11:28 AM
Post #1

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 3


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

User is offlineProfile CardPM
+Quote Post

lockdown
RE: Memory Allocated By A DLL
6 Jan, 2008 - 11:41 AM
Post #2

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
I believe once the DLL has been called and then used the application will release the memory space automatically.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Memory Allocated By A DLL
6 Jan, 2008 - 02:47 PM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 11 times
My Contributions
Detaching the DLL won't release heap allocated resources, it must be done by the application (since probably it is using those "objects").
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Memory Allocated By A DLL
10 Jan, 2008 - 12:50 AM
Post #4

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

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

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?

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.

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.

Hence, a missing FreeLibrary() call would prevent the DLL from being unmapped even though it is not in use.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Memory Allocated By A DLL
10 Jan, 2008 - 03:20 AM
Post #5

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 11 times
My Contributions
Excellent explanation! It was on my mind for a while to come back and go into more depth, but you were quicker!
User is offlineProfile CardPM
+Quote Post

metty
RE: Memory Allocated By A DLL
10 Jan, 2008 - 03:04 PM
Post #6

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 3

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
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Memory Allocated By A DLL
11 Jan, 2008 - 10:59 AM
Post #7

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

metty
RE: Memory Allocated By A DLL
12 Jan, 2008 - 10:21 AM
Post #8

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 3

QUOTE(born2c0de @ 11 Jan, 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.


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

User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Memory Allocated By A DLL
15 Jan, 2008 - 12:54 AM
Post #9

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 12:39PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month