Subject Re: [IBO] FBServer has a GPFault (Whats wrong with my code)
Author Helen Borrie
At 12:32 PM 29/03/2003 +0530, you wrote:
>I face a strange problem.
>Sometimes Firebird server crashes with an access violation.
>(This happens under Windows 98 as well Windows 2000)
>I am using FB Server 1.0 Build 794.
>It is something that I am doing in my code which is causing this problem.
>It is replicable at my end if certain conditions are met.
>There are four sections of code interacting which causes the condition to
>be met.
>I am including the four sections
>In section one I am creating a connection to the database.
>After that now under 98 if i click on fbserver icon on the tray I can see
>the number of attachments.
>Firebird properties page now show number of attachments is 1
>Section one
> try
> If Not IB_Connection1.Connected then
> IB_Connection1.Connect;
> except
> on e:EIB_ISCError do
> HandleAllErrs(self,e);
> on e:Exception do
> Raise;
> ENd;
>Section two is first creating a transaction.
>Section two
>TranMain is a global variable
>if not Assigned(TranMain) then
> TranMain:=TIBOTransaction.Create(self);
>TranMain.IB_Connection := IB_Connection1;
>TranMain.ExecuteImmediate('update tblconfiguration set NUSERS = ' +
>InttoStr(NumUsers) + ' , NTASKS=0, NMESSAGES=0');
>TranMain.CommitRetaining ;
>Section three is used to retrieve some information.
>Section 2
> Rs:TIBOQuery;
> Rs:=nil;
> Rs:=TIBOQuery.Create(self);
> Rs.IB_Connection := IB_Connection1;
> rs.SQL.Add('select DBVersion,PersonaldbVersion from tbldbconfig');
> rs.Prepare;
> Rs.Open;
> if rs.RecordCount>0 then
> begin
> <do something with info from rs>
> end;
> rs.Close;
> if assigned(rs) then freeandnil(rs);
>I expect that section 2 and section 3 code should not increase the number
>of attachments as shown on the firebird properties page.
>This is true only Sometimes .
>in most of the time the line
>TranMain.ExecuteImmediate('update tblconfiguration set NUSERS = ' +
>InttoStr(NumUsers) + ' , NTASKS=0, NMESSAGES=0');
>or the line
>causes the number of attachments to increase (+1 each).
>Not able to identify when. I am just going through the debugger. No change
>in the code but sometimes attachments will show 1, sometimes it will show
>morethan that
>If the number of attachments are morethan one then a line from Section 4
>will cause Firebird server to give a gp fault and shut down
>Section 4
> IBDB:TIBDatabase;
> DbInfo:TIBDatabaseInfo;
> SParams:Tstrings;
> DbSize:int64;
> IBDB:=nil;
> DbInfo:=nil;
> DbSize:=0;
> IBDB:=TIBDatabase.Create(nil);
> SParams:=TStringList.Create;
> SParams.Add('user_name=sysdba');
> SParams.Add('password=masterkey');
> with IBDB Do
> Begin
> DatabaseName :=RServerIP + ':' +
> IncludeTrailingPathDelimiter(RDBPath) + RDBName;
> Params:=SParams;
> AllowStreamedConnected:=false;
> LoginPrompt:=false;
> Open ;
> End;
> DbInfo := TIBDatabaseInfo.Create(nil);
> DbInfo.Database := IBDb;
> try
> CurrentNoOFDBCxns := DbInfo.UserNames.Count-1;
> Except on e:exception Do
> Application.terminate;
> End;
> On E:EIBInterBaseError Do // 336331022
> raise EIBInterbaseError.Create(e.SQLCode,e.IBErrorCode,'['+
> inttostr(e.IBErrorCode) + ']' + e.Message);
> on E:Exception Do
> Raise Exception.Create(e.Message);
> if Assigned(IBDB) then
> Begin
> if IBDB.Connected then
> IBDB.Connected:=false;
> freeandnil(IBDB);
> End;
> if Assigned(DbInfo) then
> FreeandNil(DbInfo);
> If Assigned(sPArams) then
> FreeAndnil(sparams);
>The exact line which will cause Firebird Server to crash is the following line
>CurrentNoOFDBCxns := DbInfo.UserNames.Count-1;
>Any help much appreciated.
>Can is cross post this in ib-support

No, it's a problem with your application, not a database problem. If you
post in ib-support I will move you back here.

My initial observation of your code is that it is an unholy jumble of
native IBO, TDataset-compatible IBO and IBXpress. IOW, your AVs are coming
from your Delphi application - most likely from the utter confusion of
connections and transactions you have there....and that's only in the code
we can actually *see*.

Not sure why you need to drag in an IBDatabase in Section 4 (which will
certainly give you a new connection each time you create it); but in that
block of code you have three try.. keywords + one except and one finally,
i.e. you have one improperly finished try block.

Also in that section you are referencing two objects before they have been
Both would cause an AV but the first one will crash the program before the
second one gets a chance to do it.

This looks weird too:
CurrentNoOFDBCxns := DbInfo.UserNames.Count-1;

In any case, you can get the *correct* count without resorting to
IBX. Once you get your data access objects sorted out (using TIBODatabase
instead of TIB_Connection, you only have to read the Users property (a
TStrings) to count the usernames logged in...

That's not all, actually. The transactions are a dog's dinner...