#1 2012-10-04 13:57:59

h.hasenack
Member
From: Nijmegen, Netherlands
Registered: 2012-08-01
Posts: 173
Website

Fix AV when using "dynamic" restservers

It turns out that when destroying a TDBRestserver, the registered TSQLVVirtualtableModule 's may already be destroyed...

This causes a very ugly AV in the  destructor

destructor TSQLRestServerDB.Destroy;
var i: integer;
begin
  try
    for i := 0 to high(fRegisteredVirtualTableModule) do
      if fRegisteredVirtualTableModule[i]<>nil then
        // to avoid GPF in TSQLVirtualTable.Destroy e.g.
        fRegisteredVirtualTableModule[i].fServer := nil; // MAY VERY WELL ACCESS A DESTROYEDE COMPONENT!!!
    inherited Destroy;
  finally
    try
      fStatementCache.ReleaseAllDBStatements;
    finally
      fOwnedDB.Free;
    end;
  end;
end;

Therefor I added an empty, virtual "unregister" method to TSQLRestServer, and an overriden one to TSQLRestServerDB:

// for de-registering a viirtual table module that gets destroyed for some reason
procedure TSQLRestServerDB.UnregisterVirtualTableModule(aVirtualTableObject:TSQLVirtualTableModule);
VAR i,cnt:integer;
begin
  cnt:=0;
  for i := low(fRegisteredVirtualTableModule) to high(fRegisteredVirtualTableModule) do
  begin
    if fRegisteredVirtualTableModule[i]=aVirtualTableObject then
      fRegisteredVirtualTableModule[i]:=nil;
  end;
end;

And added a destructor to TSQLVirtualTableModule:

destructor TSQLVirtualTableModule.Destroy;
VAR i:integer;
begin
  if assigned(FServer) then
    FServer.UnregisterVirtualTableModule(self);
  inherited; // for debugging purpose
end;

And now, my ugly AV has disappeared!

My suggestion would be to use an TObjectList or TList for maitaning a list of objects rather than the fRegisteredVirtualTableModule array of objects. Also  for a 'register/Unregister kind of way to manage the list of virtual tables in a TSQLRestServerDB. Hell, why not add an indexed property VirtualTableModule[idx:integer] and a VirtualTableModuleCOunt for that matter... smile Or maybe a (protected/public) VirtualTableModuleList:TObjectList ...

Regards - Hans

Offline

#2 2012-10-05 07:26:48

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

Re: Fix AV when using "dynamic" restservers

I am still not able to find out in which circumstances when destroying a TDBRestserver, the registered TSQLVVirtualtableModule 's may already be destroyed...

But I've committed a fix if this occurs, a bit diverse than yours (just overriding TSQLVirtualTableModuleDB.Destroy is enough), but the same idea.
See http://synopse.info/fossil/info/5680d09e81

Thanks for the report.

Offline

#3 2012-10-05 08:32:25

h.hasenack
Member
From: Nijmegen, Netherlands
Registered: 2012-08-01
Posts: 173
Website

Re: Fix AV when using "dynamic" restservers

Great!

Offline

Board footer

Powered by FluxBB