December 18th, 2013, 01:44 AM
Try finally with Tstringlist
I see the accepted convention when using a Tstringlist (or other class that needs freeing) is to use
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?
December 18th, 2013, 09:02 AM
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.
Originally Posted by DLPB2
December 18th, 2013, 09:51 AM
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.
December 18th, 2013, 09:56 AM
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.
Originally Posted by dve83
December 18th, 2013, 01:59 PM
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.
December 18th, 2013, 04:33 PM
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.
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.
The code above creates 10 StringLists, and never frees any of them. Do this enough times, and your application's need for resources increases.
for I := 0 to 9 do
SL := TStringList.Create;
That is correct...
December 18th, 2013, 07:13 PM
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
I guess the above is redundant, since the memory is being reclaimed anyway
December 19th, 2013, 04:25 AM
I agree..We must take prior concern on it's enhancement and total acclaim of solutions too..