Recent

Author Topic: Create and free TStringList Question(Solved)  (Read 2305 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Create and free TStringList Question(Solved)
« on: March 24, 2023, 10:00:49 pm »
My question is on the TList1..3 Free statements. Can I Free them in any order?


 
Code: Pascal  [Select][+][-]
  1. Var
  2.   TList1: TStringList;
  3.   TList2: TStringList;
  4.   TList3: TStringList;
  5.  Begin
  6.   TList1:=TStringList.Create;
  7.   TList2=TStringList.Create;
  8.   TList3:=TStringList.Create;
  9.   Try
  10.        {Some processing using the 3 tstringlist}
  11.   finally
  12.    TList1.Free;
  13.    TList2.Free;
  14.    TList3.Free;
  15.  end;
  16.  
« Last Edit: March 25, 2023, 05:05:51 pm by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Create and free TStringList Question
« Reply #1 on: March 24, 2023, 10:28:49 pm »
In That case presented, I don't see a problem in freeing it in any order.
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Create and free TStringList Question
« Reply #2 on: March 24, 2023, 10:36:36 pm »
If you do not have special reason, do not use prefix T for vars, it's confusing.
Code: Pascal  [Select][+][-]
  1. Var
  2.   List1: TStringList;
  3.   List2: TStringList;
  4.   List3: TStringList;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Create and free TStringList Question
« Reply #3 on: March 24, 2023, 11:04:12 pm »
I think Thaddy told me on global tstringList they had to be freed in reverse order of creation. Just wondering if it makes a difference inside a function.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Create and free TStringList Question
« Reply #4 on: March 24, 2023, 11:15:22 pm »
I think Thaddy told me on global tstringList they had to be freed in reverse order of creation. Just wondering if it makes a difference inside a function.
That's just nonsense.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Create and free TStringList Question
« Reply #5 on: March 25, 2023, 10:04:27 am »
I think Thaddy told me on global tstringList they had to be freed in reverse order of creation. Just wondering if it makes a difference inside a function.
That's just nonsense.

Bart
Bart, I take offence to that, because if classes depend on each other they always need to be free'd in reverse order of creation.
That is a basic premise.
Maybe it does not count in this particular question but it is a basic rule of programming. ( I think this is allowed:  >:D >:D )

And even if it is inside a function.  O:-)
Be careful what you wish for...... It is a common error and you know better.

If there is no order in the class https://www.freepascal.org/docs-html/rtl/classes/tstrings.objects.html
will kill your program.
« Last Edit: March 25, 2023, 10:31:06 am by Thaddy »
Specialize a type, not a var.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Create and free TStringList Question
« Reply #6 on: March 25, 2023, 11:04:44 am »
Bart, I take offence to that, because if classes depend on each other they always need to be free'd in reverse order of creation.
Feel free to take offense, but nothing in the above example meets those criteria.

The following example does not crash, nor should it, ever.
Code: Pascal  [Select][+][-]
  1. program test;
  2. {$mode objfpc}
  3. {$h+}
  4.  
  5. uses
  6.   SysUtils, Classes;
  7. var
  8.   Arr: Array of TStringList;
  9.   i: Integer;
  10. begin
  11.   Arr := nil; //satisfy the compiler ;-)
  12.   SetLength(Arr,1000);
  13.   writeln('Create 1000 StringLists');
  14.   for i := Low(Arr) to High(Arr) do
  15.   begin
  16.     Arr[i] := TStringList.Create;
  17.     Arr[i].OwnsObjects := True;
  18.     Arr[i].AddObject(IntToStr(i),TObject.Create);
  19.   end;
  20.   for i := Low(Arr) to High(Arr) do if Odd(i) then Arr[i].Free;
  21.   for i := Low(Arr) to High(Arr) do if not Odd(i) then Arr[i].Free;
  22.   writeln('Ok');
  23. end.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Create and free TStringList Question
« Reply #7 on: March 25, 2023, 01:18:31 pm »
"Most" ? You have a proper degree. Who gave you that?
Specialize a type, not a var.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Create and free TStringList Question
« Reply #8 on: March 25, 2023, 01:40:04 pm »
There's absolutely nothing wrong with Bart's code. No reason to insult him.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Create and free TStringList Question
« Reply #9 on: March 25, 2023, 01:58:37 pm »
"Most" ? You have a proper degree. Who gave you that?
Whom or what are you quoting here?
"Most" of what?

If you refer to me, I don't see what my "proper degree" has anything to do with my ability to program, since my "proper degree" is not related to that at all.

Bart

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Create and free TStringList Question
« Reply #10 on: March 25, 2023, 02:41:59 pm »
"Most" ? You have a proper degree. Who gave you that?

I have a PHD! Last time I checked it, it still WORKS!
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Create and free TStringList Question
« Reply #11 on: March 25, 2023, 04:23:07 pm »
The following example does not crash, nor should it, ever.

Correct. If there are no dependencies between variables then it does not matter in which order they're freed.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Create and free TStringList Question
« Reply #12 on: March 25, 2023, 04:24:24 pm »
Correct. If there are no dependencies between variables then it does not matter in which order they're freed.

QED.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Create and free TStringList Question
« Reply #13 on: March 25, 2023, 04:37:35 pm »
There's absolutely nothing wrong with Bart's code. No reason to insult him.
I did not insult Bart. I merely put that classes should *always* be free'd in reverse order of creation and Bart knows that. Especially if objects are associated with the stringlist.

Any possible insult is caused by the Dutch culture.

https://www.youtube.com/watch?v=wrEZwe1nbBU

It is not a matter of QED it is just a confirmation that it may work in some cases: read Karl Popper. <sigh> and may catastrophically fail... which it will...
« Last Edit: March 25, 2023, 04:47:31 pm by Thaddy »
Specialize a type, not a var.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Create and free TStringList Question
« Reply #14 on: March 25, 2023, 04:46:18 pm »
I did not insult Bart.
You did insult someone in this thread, and given the reply number of that insult, it's most likely me.

I merely put that classes should *always* be free'd in reverse order of creation and Bart knows that.
And you are wrong, as PascalDragon pointed out.

Unfortunatley you seem to be unable to admit you are wrong.
At least I've never seen you do that.
(Of course there's a chance (probably p=1/+inf) that you actually are the one person that is always right.)

Bart

 

TinyPortal © 2005-2018