alogrep 0 Posted March 5 type name_struct= record fname: string; lname : string; end; arec= record names : array of name_struct ; age: array of integer; end; type records = array of arec; setlength(records,100); for x :=0 to 99 do load each records[x] When I do records:=Nil, does it free all "names" (and fname, lname) of all recs as well as the recods array itself? Share this post Link to post
DelphiUdIT 176 Posted March 5 (edited) The record generally doesn't need to be freed. The array should not to be freed setting a nil, you must use: EDIT1: MY FAULT, like @David Heffernan said in the next post setting a nil is right. EDIT: Of course only with dynamic arrays !!! SetLength(records, 0); Every single elements of the array must be released if necessary. In you case you must release ALL arrays inside the record structure: EDIT2: I FORGOT ALWAYS, AND ALSO THAT IS NOT NECESSARY LIKE @Remy Lebeau later explained. for var i := Low(records) to High(records) do begin SetLength(records[i].names, 0); SetLength(records[i].age, 0); end; Integral type and string doesn't need to be released in Delphi, this include all numerical type and string. All others types should be released before resizing the array. //Example var A: array of integer; B: array of string; C: array of TComponent; SetLength(A, 9); A[0] := 1; A[1] := 2; ..... // Do something with A[] SetLenght(A, 0); //OK, right SetLength(B, 9); B[0] := 'Ola'; B[1] := 'Bye'; ..... // Do something with B[] SetLength(B, 0); //OK, right SetLength(C, 9); C[0] := TEdit.Create(self); C[1] := TLabel.Create(self); ..... // Do something with C[] SetLength(C, 0); // NOOOOO, wrong SetLength(C, 9); C[0] := TEdit.Create(self); C[1] := TLabel.Create(self); //release every single element for var compo in C do begin if Assigned(compo) then FreeAndNil(compo); end; SetLength(C, 0); // OK, right Edited March 6 by DelphiUdIT Share this post Link to post
David Heffernan 2345 Posted March 5 37 minutes ago, DelphiUdIT said: The array should not to be freed setting a nil, you must use Wrong. Setting a dynamic array to nil is identical to setting length to 0 and identical to passing it to Finalize. 1 1 Share this post Link to post
David Heffernan 2345 Posted March 5 56 minutes ago, alogrep said: When I do records:=Nil, does it free all "names" (and fname, lname) of all recs as well as the recods array itself? Yes. Share this post Link to post
Remy Lebeau 1396 Posted March 6 (edited) 20 hours ago, alogrep said: When I do records:=Nil, does it free all "names" (and fname, lname) of all recs as well as the recods array itself? In this example, yes. Like strings, dynamic arrays are also reference-counted. When you nil a reference to a dynamic array, its refcount is decremented. When its refcount falls to 0, its contents are finalized as needed (to release their references, etc), and the array is freed from memory. Edited March 6 by Remy Lebeau 1 Share this post Link to post
DelphiUdIT 176 Posted March 6 11 hours ago, David Heffernan said: Wrong. Setting a dynamic array to nil is identical to setting length to 0 and identical to passing it to Finalize. Thanks, I really didn't know this. Share this post Link to post