Forum > Beginners

[SOLVED] Free dynamic array of objects

(1/2) > >>

pcurtis:
Consider this


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TMyType = record    abc : Integer;    def : TStringList;  end; var  MyArray : array of TMyType; implementation procedure xxx;var  iTEMP : Integer;begin  SetLength(MyArray, 1000);   for iTEMP := 0 to 999 do    begin      MyArray[iTEMP].abc := 5;      MyArray[iTEMP].def := TStringList.Create;    end;end; 
How do I free all the objects in the array?.
Is  SetLength(MyArray, 0); enough?

lucamar:
No, before that you'll have to traverse the array freeing the string lists:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  for i := Low(MyArray) to High(MyArray) do      MyArray[i].def.Free;  SetLength(MyArray, 0);

marcov:
Or use Tobjectlist, which is a class wrapper of an array that can  od it for you

Zvoni:
What about advanced records?

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.
--- End quote ---

https://wiki.freepascal.org/management_operators

--- Quote ---Each record, even non-managed or empty, that implements any management operators becomes a managed type.
--- End quote ---
Implementing a Finalize-Operator, freeing the StringList there?
You could even implement an Initialize-Operator to create the StringList there

Zvoni:
Yepp. Works.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;{$modeswitch advancedrecords}Uses  SysUtils, Classes; Type   { TMyType }   TMyType = record    abc : Integer;    def : TStringList;    class operator initialize(var ARec:TMyType);    class operator finalize(var ARec:TMytype);  end; Var  MyArray : array of TMyType;  i:Integer; { TMyType } class operator TMyType.initialize(var ARec: TMyType);begin  ARec.def:=TStringList.Create;  Writeln('Create StringList');end; class operator TMyType.finalize(var ARec: TMytype);begin  ARec.def.Free;  WriteLn('Free StringList');end; begin  SetLength(MyArray,5);  For i:=0 To High(MyArray) Do MyArray[i].abc:=i;  SetLength(MyArray,0);end. 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

Navigation

[0] Message Index

[#] Next page

Go to full version