8 Replies - 2898 Views - Last Post: 15 January 2008 - 01:54 AM Rate Topic: -----

#1 metty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-January 08

Memory allocated by a DLL

Post icon  Posted 06 January 2008 - 12:28 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Memory allocated by a DLL

#2 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

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.
Was This Post Helpful? 0
  • +
  • -

#3 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

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").
Was This Post Helpful? 0
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 175
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

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.

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.
Was This Post Helpful? 0
  • +
  • -

#5 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

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!
Was This Post Helpful? 0
  • +
  • -

#6 metty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-January 08

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
Was This Post Helpful? 0
  • +
  • -

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 175
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

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.
Was This Post Helpful? 0
  • +
  • -

#8 metty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-January 08

Re: Memory allocated by a DLL

Posted 12 January 2008 - 11:21 AM

View Postborn2c0de, 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.


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
Was This Post Helpful? 0
  • +
  • -

#9 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 175
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1