Access violation when WebBroker Destory event in Direct mode.

Discussion of open issues, suggestions and bugs regarding usage of dbExpress drivers for SQL Server in Delphi and C++Builder
Post Reply
Eden0928
Posts: 47
Joined: Sun 22 Apr 2012 14:08

Access violation when WebBroker Destory event in Direct mode.

Post by Eden0928 » Mon 25 Jun 2018 05:45

I get a Access violaction when close a WebBroker Server(Indy VCL Application).

dbExpress driver is 7.2.4 + Direct mode.
Delphi XE1

My Step:
1. Create WebBroker application use Indy VCL Application.
2. Create a TDataModule and drop a TSQLConnection / TCRSQLConnection in it.
3. Set SQLConnection.Open/Close in DataModule.OnCreate/OnDestory event.
4. WebModule default action code below:

Code: Select all

procedure TWebModule2.WebModule2DefaultHandlerAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  DataModule1.SQLConnection1.ExecuteDirect('print ''Hello'' ');
  Response.Content := '<html><heading/><body>Web Server Application</body></html>';
end;

procedure TWebModule2.WebModuleCreate(Sender: TObject);
begin
  DataModule1 := TDataModule1.Create(Self);
end;
I get a Access violation message (runtime error 216) after open Browser and connection the default page.

Problem project url: https://www.dropbox.com/s/ouxb89fw8x1fc ... 6.zip?dl=0

The code is wrong?

Stellar
Devart Team
Posts: 116
Joined: Tue 03 Oct 2017 11:00

Re: Access violation when WebBroker Destory event in Direct mode.

Post by Stellar » Tue 26 Jun 2018 14:16

This behavior does not depend on using Devart dbExpress Driver for SQL Server, you can check this using a standard DBX driver to connect to SQL Server. Unfortunately, we cannot influence this behavior.

A sample of connecting to SQL Server using a standard driver:

SQLConnection1.ConnectionName := 'MSSQLConnection';
SQLConnection1.DriverName := 'MSSQL';
SQLConnection1.LibraryName := 'dbxmss.dll';
SQLConnection1.GetDriverFunc := 'getSQLDriverMSSQL';

SQLConnection1.Params.Clear;
SQLConnection1.Params.Add('HostName=Server');
SQLConnection1.Params.Add('Database=DataBase');
SQLConnection1.Params.Add('User_Name=sa');
SQLConnection1.Params.Add('Password=pwd');
SQLConnection1.Connected := True;

Eden0928
Posts: 47
Joined: Sun 22 Apr 2012 14:08

Re: Access violation when WebBroker Destory event in Direct mode.

Post by Eden0928 » Wed 27 Jun 2018 01:16

When closing WebBroker VCL stand-alone application, debugging reveals that runing to remove the driver object reference:

destructor TDBXDelegateDriver.Destroy;
begin
if FDriver <> nil then
FDriver.RemoveReference; <-- this.
FDriver := nil;
inherited;
end;

procedure TDBXDriver.RemoveReference;
begin
TDBXDriverRegistry.DBXDriverRegistry.FDrivers.LockList; <-- DBXDriverRegistry = nil, Access violation is it.
try
dec(FReferenceCount);
if FReferenceCount < 1 then
TDBXDriverRegistry.DBXDriverRegistry.FreeDriver(Self);
finally
TDBXDriverRegistry.DBXDriverRegistry.FDrivers.UnLockList;
end;
end;

If I used Devart DBX oledb mode driver or EMBT standard dbx driver, I need CoInitialize/CoUninitialize, although could solve the problem, but I want use Direct mode.

By the way, SDAC direct mode (In Delphi 10.2.3) have the same way.

This problem is coming from EMBT?

Stellar
Devart Team
Posts: 116
Joined: Tue 03 Oct 2017 11:00

Re: Access violation when WebBroker Destory event in Direct mode.

Post by Stellar » Tue 03 Jul 2018 11:43

The issue is that the finalization sections of the DBXCommon and DBXDevartSQLServer units are executed earlier than the destructor of the TWebModule2 class. When destroying the TWebModule2 class object, the components including TSQLConnection are also removed. When destroying the TSQLConnection object, the connection is closed, but the internal DBX objects have already been freed, that's why can exception occurs.
To solve the issue, you can try adding the DBXCommon and DBXDevartSQLServer units very first in the uses clause of the project, for example:

Code: Select all

program WBError216;
{$APPTYPE GUI}

uses
  DBXCommon,
  DBXDevartSQLServer,
  Forms,
  WebReq,
  IdHTTPWebBrokerBridge,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {WebModule2: TWebModule},
  Unit3 in 'Unit3.pas' {DataModule3: TDataModule};

Eden0928
Posts: 47
Joined: Sun 22 Apr 2012 14:08

Re: Access violation when WebBroker Destory event in Direct mode.

Post by Eden0928 » Wed 04 Jul 2018 01:36

Cool! Thank you very much!

Stellar
Devart Team
Posts: 116
Joined: Tue 03 Oct 2017 11:00

Re: Access violation when WebBroker Destory event in Direct mode.

Post by Stellar » Wed 04 Jul 2018 07:55

Glad to see that the issue was resolved.
Feel free to contact us if you have any further questions about our products.

Post Reply