Board index » delphi » Freeing DB Components in a DLL causes AV's

Freeing DB Components in a DLL causes AV's

I have created a DLL which contains a
TSession,TDatabase,TQuery,TDatasource & a TDBGrid. The DLL is loaded
dynamically by the calling program, but when it is free'd I am getting
an AccessViolation. I have found that if I free each of the DB
components, and then pause for 1 second before freeing the form, the
chance of getting an AV is reduced to about 10 percent - still not good
enough. I have not placed the DB components to try to keep the number of
things that can go wrong to the minimum.

Does anyone have any idea's?

--------- Source for Test.DLL -------------------

procedure Initialise( pDataBase, pUser, pPassword : PChar ); stdcall;
var
  frmTest : TfrmTest;
begin
  frmTest := TfrmTest.Create( Application );
  with frmTest do begin
    with dbTest do begin
      Connected := FALSE;
      AliasName := pDataBase;
      Params.Clear;
      Params.Add( 'USER NAME=' + pUser );
      Params.Add( 'PASSWORD='  + pPassword );
      Connected := TRUE;
    end;
    sesTest.Active := TRUE;
    qryTest.Active := TRUE;
    frmTest.ShowModal;
  end;
end;

procedure TfrmTest.FormClose(Sender: TObject; var Action: TCloseAction);

begin
  dsTest.Enabled   := FALSE;
  qryTest.Active   := FALSE;
  dbTest.Connected := FALSE;
  sesTest.Active   := FALSE;
  Delay( 1 );                                      // without delay,
will crash every time
  Action := caFree;
end;

-------- Source for calling program ---------------------------------

procedure TfrmStub.btnStubClick(Sender: TObject);
var
  hInst    : THandle;
  fPointer : TFarProc;
  TestCall : TDLLProc;
begin
  hInst := LoadLibrary( 'Test.dll' );
  if hInst > 0 then begin
    fPointer := GetProcAddress( hInst, 'Initialise' );
    if fPointer <> nil then begin
      TestCall := TDLLProc( fPointer );
      TestCall( 'DBName', 'rwest', 'password' );
      FreeLibrary( hInst );
    end;
  end;
end;

  rwest.vcf
< 1K Download
 

Re:Freeing DB Components in a DLL causes AV's


Try using the packages for the DLL and the Application...
Quote
Robert West wrote:
> I have created a DLL which contains a
> TSession,TDatabase,TQuery,TDatasource & a TDBGrid. The DLL is loaded
> dynamically by the calling program, but when it is free'd I am getting
> an AccessViolation. I have found that if I free each of the DB
> components, and then pause for 1 second before freeing the form, the
> chance of getting an AV is reduced to about 10 percent - still not good
> enough. I have not placed the DB components to try to keep the number of
> things that can go wrong to the minimum.

> Does anyone have any idea's?

> --------- Source for Test.DLL -------------------

> procedure Initialise( pDataBase, pUser, pPassword : PChar ); stdcall;
> var
>   frmTest : TfrmTest;
> begin
>   frmTest := TfrmTest.Create( Application );
>   with frmTest do begin
>     with dbTest do begin
>       Connected := FALSE;
>       AliasName := pDataBase;
>       Params.Clear;
>       Params.Add( 'USER NAME=' + pUser );
>       Params.Add( 'PASSWORD='  + pPassword );
>       Connected := TRUE;
>     end;
>     sesTest.Active := TRUE;
>     qryTest.Active := TRUE;
>     frmTest.ShowModal;
>   end;
> end;

> procedure TfrmTest.FormClose(Sender: TObject; var Action: TCloseAction);

> begin
>   dsTest.Enabled   := FALSE;
>   qryTest.Active   := FALSE;
>   dbTest.Connected := FALSE;
>   sesTest.Active   := FALSE;
>   Delay( 1 );                                      // without delay,
> will crash every time
>   Action := caFree;
> end;

> -------- Source for calling program ---------------------------------

> procedure TfrmStub.btnStubClick(Sender: TObject);
> var
>   hInst    : THandle;
>   fPointer : TFarProc;
>   TestCall : TDLLProc;
> begin
>   hInst := LoadLibrary( 'Test.dll' );
>   if hInst > 0 then begin
>     fPointer := GetProcAddress( hInst, 'Initialise' );
>     if fPointer <> nil then begin
>       TestCall := TDLLProc( fPointer );
>       TestCall( 'DBName', 'rwest', 'password' );
>       FreeLibrary( hInst );
>     end;
>   end;
> end;

Re:Freeing DB Components in a DLL causes AV's


Try using the packages for the DLL and the Application...
Quote
Robert West wrote:
> I have created a DLL which contains a
> TSession,TDatabase,TQuery,TDatasource & a TDBGrid. The DLL is loaded
> dynamically by the calling program, but when it is free'd I am getting
> an AccessViolation. I have found that if I free each of the DB
> components, and then pause for 1 second before freeing the form, the
> chance of getting an AV is reduced to about 10 percent - still not good
> enough. I have not placed the DB components to try to keep the number of
> things that can go wrong to the minimum.

> Does anyone have any idea's?

> --------- Source for Test.DLL -------------------

> procedure Initialise( pDataBase, pUser, pPassword : PChar ); stdcall;
> var
>   frmTest : TfrmTest;
> begin
>   frmTest := TfrmTest.Create( Application );
>   with frmTest do begin
>     with dbTest do begin
>       Connected := FALSE;
>       AliasName := pDataBase;
>       Params.Clear;
>       Params.Add( 'USER NAME=' + pUser );
>       Params.Add( 'PASSWORD='  + pPassword );
>       Connected := TRUE;
>     end;
>     sesTest.Active := TRUE;
>     qryTest.Active := TRUE;
>     frmTest.ShowModal;
>   end;
> end;

> procedure TfrmTest.FormClose(Sender: TObject; var Action: TCloseAction);

> begin
>   dsTest.Enabled   := FALSE;
>   qryTest.Active   := FALSE;
>   dbTest.Connected := FALSE;
>   sesTest.Active   := FALSE;
>   Delay( 1 );                                      // without delay,
> will crash every time
>   Action := caFree;
> end;

> -------- Source for calling program ---------------------------------

> procedure TfrmStub.btnStubClick(Sender: TObject);
> var
>   hInst    : THandle;
>   fPointer : TFarProc;
>   TestCall : TDLLProc;
> begin
>   hInst := LoadLibrary( 'Test.dll' );
>   if hInst > 0 then begin
>     fPointer := GetProcAddress( hInst, 'Initialise' );
>     if fPointer <> nil then begin
>       TestCall := TDLLProc( fPointer );
>       TestCall( 'DBName', 'rwest', 'password' );
>       FreeLibrary( hInst );
>     end;
>   end;
> end;

Other Threads