Page 1 of 1

Access violation when WebBroker Destory event in Direct mode.

Posted: Mon 25 Jun 2018 05:45
by Eden0928
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?

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

Posted: Tue 26 Jun 2018 14:16
by Stellar
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;

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

Posted: Wed 27 Jun 2018 01:16
by Eden0928
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?

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

Posted: Tue 03 Jul 2018 11:43
by Stellar
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};

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

Posted: Wed 04 Jul 2018 01:36
by Eden0928
Cool! Thank you very much!

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

Posted: Wed 04 Jul 2018 07:55
by Stellar
Glad to see that the issue was resolved.
Feel free to contact us if you have any further questions about our products.