Menu

Raise Exception in Finalization section

a2z
2004-04-08
2004-04-09
  • a2z

    a2z - 2004-04-08

    Hi,

    Having gone throught the sources, I'd like to point out a couple of things:

    Raising Exception in Finalization sections is akin to disaster waiting to happen. Thereason is, since the Exception is being raised in Finalization section and since Finalization section is that --Finalization--, there is no way you will catch that Exception.. I have had this gotcha in the past and I feel the need to warn you against that.

    Indeed, you really dont need those AllocCount stuff as global variables. It is best to put them as private variables in TSLEngine and then have 3 procedures in TSLEngine, such as:

        {$IFDEF COUNT_DATA_ALLOC}
        Procedure AllocCountInc;
        Procedure AllocCountDec;
        Procedure ReAllocCountInc;
        {$ENDIF}

    which are implemented as

    {$IFDEF COUNT_DATA_ALLOC}

    Procedure TSLEngine.AllocCountInc;
    Begin
      Inc(FAllocCount);
    End;

    Procedure TSLEngine.AllocCountDec;
    Begin
      Dec(FAllocCount);
    End;

    Procedure TSLEngine.ReAllocCountInc;
    Begin
      Inc(FReallocCount);
    End;
    {$ENDIF}

    and the relevant other code would be

    Constructor TSLEngine.Create(aConnect: Boolean = True; Const aLibName: String = '');
    Begin
      FAllocCount := 0;
      FReallocCount := 0;
      [....]
    End;

    Destructor TSLEngine.Destroy;
    Begin
      {$IFDEF COUNT_DATA_ALLOC}
      If FAllocCount <> 0 Then Raise ESQLiteError.Create('Memory allocation error :' + IntToStr(FAllocCount));
      {$ENDIF}
      [....]
    End;

    Naturally, you would have to modify the places where those (previously) global variables were altered, such as

        {$IFDEF COUNT_DATA_ALLOC}
        FDB.Engine.AllocCountInc;
        {$ENDIF}

        {$IFDEF COUNT_DATA_ALLOC}
        FDB.Engine.AllocCountDec;
        {$ENDIF}

        {$IFDEF COUNT_DATA_ALLOC}
        FDB.Engine.ReAllocCountInc;
        {$ENDIF}

    etc.

    Doing it this way does not only remove the likelihood of getting untraceable exceptions, but it also lets you trace them in terms of the class itself.

    ----

    Finally, you are using ___Engine as a default Engine. Fine, but, it would be much better if you did not expose it as a variable. A read-only version of it is a lot safer. Such as:

    Function DefaultEngine: TSLEngine;

    And in the Implementation section,

    Var
      ___Engine: TSLEngine = Nil;
           {Set it to Nil. Just to be safe, and safe again.}

    Function DefaultEngine: TSLEngine;
    Begin
      Result := ___Engine;
    End;

    My 0.02 cyber units.

    Cheers,
    Adem

     
    • Michał Zaborowski

      Thanks for your time, cyber units, cents and for all ;)

        Raising this exception is only for developer info. Since project is in alpha stage I want all to be informed about problems. In normal app counting should be turned off. It is litte helper - just for information about memory leak.
        About your other mails -
      SLTypes - ok sorce will be moved to SLEngine.
      Location - ok, I need some tests, but it looks nice.

      Your changes and fpc compatibility will be placed in next file release. When? I have no idea :-( But I hope sooon ;)

      Micha&#322; Zaborowski aka. TeXXaS

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.