Page 1 of 1

How do I free the memory used up in loading a DLL?

Posted: Mon Jan 17, 2011 7:57 am
by Nituvious
I am trying to run an application that loads a dll into memory, runs a function inside of it named DllMain() then immediately closes the library and loads different DLL with the same function name then runs it and immediately closes the library again. However, the program uses more and more ram everytime it loads the Dll eventually using all available ram.
How do I free the used memory so I can load another DLL again without using up all of my ram?

Re: How do I free the memory used up in loading a DLL?

Posted: Mon Jan 17, 2011 11:31 am
by Trond
Maybe the dll files have a memory leak?

Re: How do I free the memory used up in loading a DLL?

Posted: Mon Jan 17, 2011 11:54 am
by Nituvious
Hmm, if the DLL allocates 1024 bytes of data and doesn't free the memory afterwards does it still continue to persist inside of the program even after the library is closed?

[Edit] I just tried with a blank DLL that opens a single messagebox..

Here is my procedure that loads the dll:
It's placed inside of the main loop and called very very often.

Code: Select all

Prototype.l protoDLLMain()
Procedure LoadDLL()
	directoryEntry = ExamineDirectory(#PB_Any,"common\","*.dll")
	While NextDirectoryEntry(directoryEntry)
		libraryEntry = OpenLibrary(#PB_Any,"common\"+DirectoryEntryName(directoryEntry))
		DLLMain.protoDLLMain = GetFunction(libraryEntry,"DLLMain")
		DLLMain()
		CloseLibrary(libraryEntry)
	Wend
	FinishDirectory(directoryEntry)
EndProcedure

Re: How do I free the memory used up in loading a DLL?

Posted: Mon Jan 17, 2011 2:57 pm
by Trond
Nituvious wrote:Hmm, if the DLL allocates 1024 bytes of data and doesn't free the memory afterwards does it still continue to persist inside of the program even after the library is closed?
Yes.

Re: How do I free the memory used up in loading a DLL?

Posted: Mon Jan 17, 2011 4:19 pm
by Nituvious
Even with just a simple messagebox DLL the program still seems to use up all of my ram eventually. I think the issue is my loadDLL function. Any ideas?

Re: How do I free the memory used up in loading a DLL?

Posted: Tue Jan 18, 2011 11:20 am
by Nituvious
Ugh, I still can't seem to fix this issue. I think the issue is within my loadDLL function since loading a simple messagebox dll still causes a memory leak(if that's even the term for this).

Re: How do I free the memory used up in loading a DLL?

Posted: Tue Jan 18, 2011 11:56 am
by Trond
I can confirm this. It looks like the use of strings inside the dll is the problem. It must be a bug in PB.

Re: How do I free the memory used up in loading a DLL?

Posted: Tue Jan 18, 2011 12:05 pm
by Nituvious
Maybe I can rely on API to get over this bug?

Re: How do I free the memory used up in loading a DLL?

Posted: Tue Jan 18, 2011 12:09 pm
by Trond
The simple way would be to only load each dll once.

Re: How do I free the memory used up in loading a DLL?

Posted: Tue Jan 18, 2011 12:29 pm
by Nituvious
Trond wrote:The simple way would be to only load each dll once.
I wanted to do it that way originally but I can't because the loaded dll's should be removed or altered while the host program is running. Additional libraries can also be added while the program runs as well.

Re: How do I free the memory used up in loading a DLL?

Posted: Tue Jan 18, 2011 5:02 pm
by Nituvious
Do you think I could fix this memory leak by putting the function that loads the DLL in a thread and then ending that thread after the function has called? I haven't had a chance to try yet, I will when I get home though.

Re: How do I free the memory used up in loading a DLL?

Posted: Tue Jan 18, 2011 5:17 pm
by Trond
No.

Re: How do I free the memory used up in loading a DLL?

Posted: Tue Jan 18, 2011 8:55 pm
by IceSoft
If it is a PB bug -> write a bug message.

Re: How do I free the memory used up in loading a DLL?

Posted: Wed Jan 19, 2011 7:05 pm
by kandl
i dont think it's really a bug in PB, because you didn't really free the library; need to call FreeLibrary() in win32

just have to make your own FreeLibrary() procedure

Code: Select all

Procedure FreeLibrary(library_no)
  
  FreeLibrary_(LibraryID(library_no))
  
EndProcedure

Re: How do I free the memory used up in loading a DLL?

Posted: Wed Jan 19, 2011 9:08 pm
by Trond
kandl: You don't need to use FreeLibrary_() when you use CloseLibrary(), and even with FreeLibrary_() the memory usage grows in the same fashion.