#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    15
    Rep Power
    0

    Try finally with Tstringlist


    I see the accepted convention when using a Tstringlist (or other class that needs freeing) is to use

    Code:
    List:=Tstringlist.create;
    TRY
    DO something
    FINALLY
    List.free;
    end;
    My question is, is TRY FINALLY always needed? I assume that if the "do something" is simple enough and won't bring up an exception, TRY FINALLY is just being paranoid?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    356
    Rep Power
    7
    Originally Posted by DLPB2
    I see the accepted convention when using a Tstringlist (or other class that needs freeing) is to use

    Code:
    List:=Tstringlist.create;
    TRY
    DO something
    FINALLY
    List.free;
    end;
    My question is, is TRY FINALLY always needed? I assume that if the "do something" is simple enough and won't bring up an exception, TRY FINALLY is just being paranoid?
    I guess that depends on - what the definition of "needed" is... Obviously the code will more than likely do what you want it to do, but is that prudent. Much like you don't need to look both ways when crossing the street, but it sure is prudent.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    South Africa
    Posts
    59
    Rep Power
    11
    I agree. Try Finally End (together with creates and frees) may not be necessary for a particular instance (eg. if you have very simplified code in there), but it remains good practice. Tomorrow some other developer changes your "do something code" and doesn't worry about the rest - resulting in problems if you dont have a try finally.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    356
    Rep Power
    7
    Originally Posted by dve83
    Tomorrow some other developer changes your do something code
    That's a very good point... the code your write today will be modified sooner or later, and not always by you. Any "simple" piece of code can one day become very complex.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    15
    Rep Power
    0
    Cheers!

    1. Is there a performance hit to using TRY FINALLY over a block of code that will be in a loop?

    2. Also may as well get an unrelated question out of the way too... "free" calls.

    I am told always when I create something I must free it. But what if the application will be ending when it is freed? Let's say I have a StringList and it has to remain open for the duration of the application... do I need to free it on form close? Or will all memory that it used be returned when the program is closed (by the user clicking the X).

    Is it necessary to free things when the application will be ending at the time you need to free it?

    3. Similarly, "cafree"... I take it this is not needed on the main form, because when the user clicks close, it's freed automatically.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    356
    Rep Power
    7
    Is there a performance hit to using TRY FINALLY over a block of code that will be in a loop?
    Yes, technically a try..finally and try..except both have a performance issue, but unless you are doing it within a loop, you probably won't be able to discern the difference.
    Is it necessary to free things when the application will be ending at the time you need to free it?
    Well, technically the operating system will reclaim the memory leaked during the execution of your program when it termates, but if you continue to leak memory, and your application runs for an extended period of time, you do run the risk of running out of resources.
    e.g.
    Code:
    var
       SL: TStringList;
       I: Integer;
    begin
       for I := 0 to 9 do
       begin
          SL := TStringList.Create;
          SL.Add(IntToStr(I));
          SL.SaveToFile('MyFile.Txt');
       end;
    end;
    The code above creates 10 StringLists, and never frees any of them. Do this enough times, and your application's need for resources increases.
    "cafree"... I take it this is not needed on the main form, because when the user clicks close, it's freed automatically.
    That is correct...
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    15
    Rep Power
    0
    Thank you I have coded for quite a while, but I have never really found answers to things we take for granted. For example, I have always freed things thoroughly, but recently I created a console app program that terminates if a stringlist is empty and so I wondered... do I still need to free it at that point...

    if list.count=0 then
    begin
    list.free;
    exit;
    end;

    I guess the above is redundant, since the memory is being reclaimed anyway
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0
    I agree..We must take prior concern on it's enhancement and total acclaim of solutions too..

IMN logo majestic logo threadwatch logo seochat tools logo