VISOCO Software  Home | Products | Services | Download | Order | Support | Forum | Resources | Search | About
  RSS feedRSS Feed  FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups  RegisterRegister   ProfileProfile    Log inLog in 
Library Handle resource link using TSQLConnection

 
Post new topic   Reply to topic    VISOCO Software Forum Index -> VISOCO dbExpress driver for Sybase ASE
Author Message
bmarinel



Joined: 03 Apr 2003
Posts: 18

PostPosted: Fri Apr 11, 2003 6:24 pm    Post subject: Library Handle resource link using TSQLConnection Reply with quote

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
View user's profile Send private message
VISOCO Support
VISOCO Software Support


Joined: 16 Jul 2002
Posts: 96

PostPosted: Sun Apr 13, 2003 12:22 am    Post subject: Library Handle resource leak Reply with quote

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 Wink.
Where are calls of FreeLibrary?

Best wishes.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
bmarinel



Joined: 03 Apr 2003
Posts: 18

PostPosted: Sun Apr 13, 2003 3:15 am    Post subject: Library Handle resource leak Reply with quote

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
View user's profile Send private message
VISOCO Support
VISOCO Software Support


Joined: 16 Jul 2002
Posts: 96

PostPosted: Mon Apr 14, 2003 9:07 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
bmarinel



Joined: 03 Apr 2003
Posts: 18

PostPosted: Tue Apr 15, 2003 1:29 pm    Post subject: Library Handle resource leak Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    VISOCO Software Forum Index -> VISOCO dbExpress driver for Sybase ASE All times are GMT + 2 Hours
Page 1 of 1

 


©VISOCO Software. phpBB by phpBB Group