Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Freeing DLL's

Status
Not open for further replies.

EricDraven

Programmer
Jan 17, 2002
2,999
0
0
GB
Hi all,

Because quickreports combined with all the code required to generate my reports adds so much to the executable size, I have set up every report as a DLL. The DLL's are called using the following code

var
Listing : procedure (My Variable);
MyLib : THandle;
begin
MyLib := LoadLibrary(PChar(ExtractFilePath(Application.Exename) + 'MyDLL.dll'));
if (MyLib <> 0) then
begin
try
@Listing := GetProcAddress(MyLib, PChar('Listing'));
if Assigned(Listing)then Listing(My Variables);
finally
FreeLibrary(MyLib);
end;
end;
end;

This loads the DLLs fine, but after a while users are beginning to get errors in the main application (i.e Table is Busy). All of my DLL's access tables and I create 1 or more forms in the called DLL procedure for quickreports etc.

What is the correct procedure in the DLL for closing and freeing it from memory? Or is there something amiss with the way I call the DLL's (I am aware of ShareMem). One such error has just occured after repeatedly opening and closing the DLL's stating that there have been too many exceptions??? Another thing worth mentioning is that one of my DLL's waits for user interaction before previewing the QuickRep and this DLL can be closed and opened as many times as I wish provided the QuickRep isnt previewed???


Arte Et Labore
 
hi Eric,

why do You want to unload the dll at once after calling the exported function ? This could induce perfomance problems as well, I guess.
I normally call FreeLibrary at main program shutdown. But it should be possible to not call FreeLibrary at all and just let the OS do the freeing of memory.

Anyway I found the unloading a dll which has forms which are currently open often did cause exceptions. So before unloading the dll I call another exported procedure to close the forms of the dll.

regards
Roderich
 
I am freeing the DLL's straight after because all the DLL does is create a quickreport preview. Once the preview is closed there is no need for the DLL to remain using up memory so it is closed until the user needs that specific report again. Because of the amount of specific reports and the amount of code that goes with them all, I cannot have each DLL created at the start and then freed when closing the main application.

Arte Et Labore
 
Okay,

I have managed to solve the problem mentioned above to an extent. Now the only time my program crashes is when I call a DLL and then is an error in the called prcedure.

I have placed the code in my DLL's procedure into a try except, but am unsure of what to put in the except statement. Should I be freeing something here or will it automatically be destroyed having failed to run correctly! (Incidently, the errors which cause the except statement to fire only occur because fields may be missing in tables used to calculate parts of my report)

Any help on this matter would be greatly appreciated!



Arte Et Labore
 
Its been a couple of years since I worked with Delphi so anyone correct me if I'm wrong here.

Your errors would suggest to me that something is not being freed properly or the DLL is not being called correctly. Depending on the size of the DLL, I would prefer to keep it in memory and only free it when the program is closed.

Is @Listing treated as a pointer and if so does it need to be freed?

Is using GetProcAddress the most appropriate method of accessing the DLL? There were other ways of doing this in past Delphi versions, but I can't remember if this allowed dynamic load/unload of a DLL.

Reading the other posts also makes me think, do you have a proper Exit procedure set up for the DLL? This should close/free any resources created/opened by the DLL (but not needed outside it) such as forms, pointers etc.

I don't have Delphi installed on this PC so can't check for you and don't have any of my code either.

Which version of Delphi are you using anyway?
 
Thanks for your response, I have muddled on through and from various posts in the borland news groups I was able to see that I wasnt freeing the DLL correctly. I didnt have an Exit procedure setup for each DLL which I have now remedied.

Arte Et Labore
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top