#1 2014-10-26 08:04:45

alexdmatveev
Member
Registered: 2014-09-12
Posts: 87

Exception in Destroy in FMX project

Hello,

I have next servers/client/models:

  FClient := TSQLHttpClientWinHTTP.Create(FServer, FPort, Model);
  FDocumentClient := TSQLHttpClientWinHTTP.Create(FDocumentServer, FDocumentPort, DocumentModel);
  FLocalModel := LocalModel;
  FLocalServer := TSQLRestServerDB.Create(FLocalModel, ChangeFileExt(paramstr(0),'.db3'));
  FLocalServer.CreateMissingTables;
  FLocalClient := TSQLRestClientDB.Create(FLocalServer);

they are created and work good.

destructor TOmConnectionManager.Destroy;
begin
  if sqlite3 = nil then
    sqlite3 := TSQLite3LibraryStatic.Create;

  FreeAndNil(FModel);
  FreeAndNil(FClient);

  FLocalClient.Free;
  FLocalServer.Free;
  FLocalModel.Free;

  FDocumentClient.Free;
  FDocumentModel.Free;

  FreeAndNil(sqlite3);
  inherited;
end;

Then I try to destroy them and always get exception in mORMot.pas

destructor TSQLModel.Destroy;
var i,j: integer;
begin
  for i := 0 to fTablesMax do begin
    with TableProps[i].Props do begin
      EnterCriticalSection(fLock); // may be called from several threads at once   <---- THEN EXCEPTION HERE
      try
        for j := 0 to fModelMax do
          if fModel[j].Model=self then begin
            // un-associate this TSQLRecord with this model
            Move(fModel[j+1],fModel[j],(fModelMax-j)*sizeof(fModel[j]));
            dec(fModelMax);
            break;
          end;
        TableProps[i].Free;
      finally
        LeaveCriticalSection(fLock);
      end;
    end;
  end;
  inherited;
end;

in VCL everything works good as I see. The problem is in FMX project.

Pls show me the way. Thanks.

Offline

#2 2014-10-26 21:00:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Exception in Destroy in FMX project

As with any object, you have to destroy them in the reverse order than your creation.
Try to destroy FModel AFTER FClient.

It works "by change" under VCL.

And, BTW what are you doing with the "sqlite3" global variable in your destructor?
Leave the poor variable alone!
smile

Offline

#3 2016-01-14 13:20:19

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Re: Exception in Destroy in FMX project

destructor TSQLModel.Destroy;
var i,j: integer;
begin
  for i := 0 to fTablesMax do begin
    with TableProps[i].Props do begin
      EnterCriticalSection(fLock); // may be called from several threads at once   <---- THEN EXCEPTION HERE
      try
        for j := 0 to fModelMax do
          if fModel[j].Model=self then begin
            // un-associate this TSQLRecord with this model
            Move(fModel[j+1],fModel[j],(fModelMax-j)*sizeof(fModel[j]));
            dec(fModelMax);
            break;
          end;
        TableProps[i].Free;
      finally
        LeaveCriticalSection(fLock);
      end;
    end;
  end;
  inherited;
end;

Is it okay when "EnterCriticalSection(fLock)" in the code above accesses the fLock property of the class TSQLRecordProperties?! Or is it a bug?

  TSQLRecordProperties = class
  protected
    ...
    fLock: TRTLCriticalSection;
    ...
  public

Last edited by cypriotcalm (2016-01-14 13:21:37)

Offline

#4 2016-01-14 19:51:21

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Exception in Destroy in FMX project

This is on purpose.
The lock is to safely access the fModel[] array.

The code is correct, the problem is in the user code, as I wrote above:

As with any object, you have to destroy them in the reverse order than your creation.
Try to destroy FModel AFTER FClient.

Offline

Board footer

Powered by FluxBB