| Author |
Message |
bmarinel
Joined: 03 Apr 2003 Posts: 18
|
Posted: Fri Apr 11, 2003 6:24 pm Post subject: Library Handle resource link using TSQLConnection |
|
|
Hi,
I'm using Memory Sleuth v3.01 with Delphi 7 to check my application for memory and resource leaks.
Memory Sleuth indicates a Library Handle resource leak whenever I use the TSQLConnection component. I've indicated the source of the leak in the code segment below.
Code segment from Delphi7\Source\Vcl\SqlExpr.pas:
procedure TSQLConnection.LoadSQLDll;
begin
if DllHandle = THandle(0) then
begin
try
SetCursor(HourGlassCursor);
if SQLDllHandle = THandle(0) then
--> SQLDllHandle := THandle(LoadLibrary(PChar(trim(LibraryName))));
if SQLDllHandle = THandle(0) then
DataBaseErrorFmt(sDLLLoadError, [LibraryName]);
GetDriver := GetProcAddress(HMODULE(SQLDllHandle), PChar(trim(GetDriverFunc)));
if not Assigned(GetDriver) then
DataBaseErrorFmt(sDLLProcLoadError, [GetDriverFunc])
finally
SetCursor(DefaultCursor);
end;
end;
end;
My question is, how do I fix this Library Handle resource link?
Regards,
Bill |
|
| Back to top |
|
 |
VISOCO Support VISOCO Software Support
Joined: 16 Jul 2002 Posts: 96
|
Posted: Sun Apr 13, 2003 12:22 am Post subject: Library Handle resource leak |
|
|
Hello.
See also SqlExpr.pas, line 1911, TSQLConnection.DoDisconnect
| Code: |
...
SQLDllHandle := THandle(0);
...
|
There is nothing more! Think, Borland dbExpress developers must be asked first .
Where are calls of FreeLibrary?
Best wishes. |
|
| Back to top |
|
 |
bmarinel
Joined: 03 Apr 2003 Posts: 18
|
Posted: Sun Apr 13, 2003 3:15 am Post subject: Library Handle resource leak |
|
|
Hi,
I wouldn't expect, or want, them to call FreeLibrary at disconnect time. I just want to call it when my program exits. I made an attempt to call it, but it must have not been correct because Memory Sleuth still said that I had the resource leak. Do you know what code I can put in my application at termination time to free the resource?
Regards,
Bill |
|
| Back to top |
|
 |
VISOCO Support VISOCO Software Support
Joined: 16 Jul 2002 Posts: 96
|
Posted: Mon Apr 14, 2003 9:07 am Post subject: |
|
|
Hello.
You can try something like:
| Code: |
procedure FreeDriver;
var
DLL: THandle;
begin
DLL := GetModuleHandle('dbexpsyb.dll');
FreeLibrary(DLL); // One or more times
end;
|
But there is a problem with library reference counting. SQLConnection creates additional clones internally and you cannot know how much times LoadLibrary was called.
So the best solution will be (SqlExpr.pas, line 1911):
| Code: |
procedure TSQLConnection.DoDisconnect;
begin
if FSQLDriver <> nil then
begin
ConnectionState := csStateDisconnecting;
CloseDataSets;
RegisterTraceCallback(False);
if (FSQLMetaData <> nil) then
FSQLMetaData := nil;
if (FISQLConnection <> nil) then
begin
FISQLConnection.disconnect;
FTransactionCount := 0;
FISQLConnection := nil;
end;
ConnectionState := csStateClosed;
FSQLDriver := nil;
FActiveStatements := 0;
FreeLibrary(SQLDllHandle);
SQLDllHandle := THandle(0);
end;
FParamsLoaded := False;
end;
|
|
|
| Back to top |
|
 |
bmarinel
Joined: 03 Apr 2003 Posts: 18
|
Posted: Tue Apr 15, 2003 1:29 pm Post subject: Library Handle resource leak |
|
|
Thank you. I ended up using both code samples. Just adding in the code to the TSQLConnection.DoDisconnect procedure didn't always do the trick. I had to add this to one application:
AttemptCount := 0;
while (GetModuleHandle(PChar(MyConnect.LibraryName)) > 0) and
(AttemptCount < MaxAttempts) do
begin
FreeLibrary(GetModuleHandle(PChar(MyConnect.LibraryName)));
Inc(AttemptCount);
end;
Regards,
Bill |
|
| Back to top |
|
 |
|