### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [SOLVED] Free dynamic array of objects  (Read 1078 times)

#### pcurtis

• Hero Member
• Posts: 634
##### [SOLVED] Free dynamic array of objects
« on: May 26, 2021, 10:48:45 am »
Consider this

Code: Pascal  [Select][+][-]
1. type
2.   TMyType = record
3.     abc : Integer;
4.     def : TStringList;
5.   end;
6.
7. var
8.   MyArray : array of TMyType;
9.
10. implementation
11.
12. procedure xxx;
13. var
14.   iTEMP : Integer;
15. begin
16.   SetLength(MyArray, 1000);
17.
18.   for iTEMP := 0 to 999 do
19.     begin
20.       MyArray[iTEMP].abc := 5;
21.       MyArray[iTEMP].def := TStringList.Create;
22.     end;
23. end;
24.

How do I free all the objects in the array?.
Is  SetLength(MyArray, 0); enough?
« Last Edit: May 26, 2021, 12:23:23 pm by pcurtis »
Windows 10
Laz 2.0.10
FPC 3.2.0

#### lucamar

• Hero Member
• Posts: 4167
##### Re: Free dynamic array of objects
« Reply #1 on: May 26, 2021, 11:02:01 am »
No, before that you'll have to traverse the array freeing the string lists:

Code: Pascal  [Select][+][-]
1.   for i := Low(MyArray) to High(MyArray) do
2.       MyArray[i].def.Free;
3.   SetLength(MyArray, 0);
« Last Edit: May 26, 2021, 11:04:22 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### marcov

• Hero Member
• Posts: 9458
• FPC developer.
##### Re: Free dynamic array of objects
« Reply #2 on: May 26, 2021, 11:04:45 am »
Or use Tobjectlist, which is a class wrapper of an array that can  od it for you

#### Zvoni

• Hero Member
• Posts: 627
##### Re: Free dynamic array of objects
« Reply #3 on: May 26, 2021, 11:10:01 am »

From SetLength
https://www.freepascal.org/docs-html/rtl/system/setlength.html
Quote
The elements that fall outside the new length are finalized if the array element is a managed type.

https://wiki.freepascal.org/management_operators
Quote
Each record, even non-managed or empty, that implements any management operators becomes a managed type.
Implementing a Finalize-Operator, freeing the StringList there?
You could even implement an Initialize-Operator to create the StringList there
« Last Edit: May 26, 2021, 11:12:18 am by Zvoni »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

#### Zvoni

• Hero Member
• Posts: 627
##### Re: Free dynamic array of objects
« Reply #4 on: May 26, 2021, 11:51:49 am »
Yepp. Works.

Code: Pascal  [Select][+][-]
1. program Project1;
3. Uses
4.   SysUtils, Classes;
5.
6. Type
7.
8.   { TMyType }
9.
10.   TMyType = record
11.     abc : Integer;
12.     def : TStringList;
13.     class operator initialize(var ARec:TMyType);
14.     class operator finalize(var ARec:TMytype);
15.   end;
16.
17. Var
18.   MyArray : array of TMyType;
19.   i:Integer;
20.
21. { TMyType }
22.
23. class operator TMyType.initialize(var ARec: TMyType);
24. begin
25.   ARec.def:=TStringList.Create;
26.   Writeln('Create StringList');
27. end;
28.
29. class operator TMyType.finalize(var ARec: TMytype);
30. begin
31.   ARec.def.Free;
32.   WriteLn('Free StringList');
33. end;
34.
35. begin
36.   SetLength(MyArray,5);
37.   For i:=0 To High(MyArray) Do MyArray[i].abc:=i;
38.   SetLength(MyArray,0);
39. end.
40.
HeapTrc throws no memory-leak

Returns in console.
Create StringList
Create StringList
Create StringList
Create StringList
Create StringList
Free StringList
Free StringList
Free StringList
Free StringList
Free StringList
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

#### pcurtis

• Hero Member
• Posts: 634
##### Re: Free dynamic array of objects
« Reply #5 on: May 26, 2021, 12:22:39 pm »
Thanks all.
Windows 10
Laz 2.0.10
FPC 3.2.0