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

Just starting out? Need help? Post your questions and find answers here.
Nituvious
Addict
Addict
Posts: 1028
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

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

Post 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?
▓▓▓▓▓▒▒▒▒▒░░░░░
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

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

Post by Trond »

Maybe the dll files have a memory leak?
Nituvious
Addict
Addict
Posts: 1028
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

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

Post 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
▓▓▓▓▓▒▒▒▒▒░░░░░
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

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

Post 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.
Nituvious
Addict
Addict
Posts: 1028
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

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

Post 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?
▓▓▓▓▓▒▒▒▒▒░░░░░
Nituvious
Addict
Addict
Posts: 1028
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

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

Post 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).
▓▓▓▓▓▒▒▒▒▒░░░░░
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

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

Post 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.
Nituvious
Addict
Addict
Posts: 1028
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

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

Post by Nituvious »

Maybe I can rely on API to get over this bug?
▓▓▓▓▓▒▒▒▒▒░░░░░
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

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

Post by Trond »

The simple way would be to only load each dll once.
Nituvious
Addict
Addict
Posts: 1028
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

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

Post 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.
▓▓▓▓▓▒▒▒▒▒░░░░░
Nituvious
Addict
Addict
Posts: 1028
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

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

Post 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.
▓▓▓▓▓▒▒▒▒▒░░░░░
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

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

Post by Trond »

No.
User avatar
IceSoft
Addict
Addict
Posts: 1690
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

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

Post by IceSoft »

If it is a PB bug -> write a bug message.
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
kandl
User
User
Posts: 11
Joined: Sat Apr 11, 2009 6:54 am

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

Post 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
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

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

Post 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.
Post Reply