* * *

Author Topic: Dynamic array cleanup  (Read 1079 times)

j831526

  • Newbie
  • Posts: 2
Dynamic array cleanup
« on: August 27, 2017, 01:49:40 am »
I define ARRAY OF String
where all strings are ANSI strings set by compiler option $H+
I wish to reclaim memory when the array is no longer needed.
My understanding is Setlength(my_array, 0) frees the array memory, but what about the string entries? Does this set all their reference counts to 0, or do they become orphans?

Thanx - Charlie

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 4376
    • wiki
Re: Dynamic array cleanup
« Reply #1 on: August 27, 2017, 03:31:29 am »
It will decrease their ref counts, (even in nested arrays).

If no other variable holds on to the string, then they are freed

j831526

  • Newbie
  • Posts: 2
Re: Dynamic array cleanup
« Reply #2 on: August 27, 2017, 03:50:59 am »
Thanx Martin.

ASerge

  • Sr. Member
  • ****
  • Posts: 450
Re: Dynamic array cleanup
« Reply #3 on: August 27, 2017, 12:06:07 pm »
Short form for SetLength(my_array, 0); is my_array := nil;

Thaddy

  • Hero Member
  • *****
  • Posts: 4617
Re: Dynamic array cleanup
« Reply #4 on: August 27, 2017, 12:13:06 pm »
Short form for SetLength(my_array, 0); is my_array := nil;
No, it isn't. Not if the array is of a managed type. That's a plain silly answer and wrong.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

ASerge

  • Sr. Member
  • ****
  • Posts: 450
Re: Dynamic array cleanup
« Reply #5 on: August 27, 2017, 12:53:07 pm »
No, it isn't. Not if the array is of a managed type. That's a plain silly answer and wrong.
Provide an example where this is not the same before throwing charges.

howardpc

  • Hero Member
  • *****
  • Posts: 2388
Re: Dynamic array cleanup
« Reply #6 on: August 27, 2017, 01:04:46 pm »
It is a plain answer, and right, both for ordinal and managed types.
Code: Pascal  [Select]
  1. program NillingDynamicArrayToZeroItsLength;
  2.  
  3. type
  4.   TIntArray = array of Integer;
  5.   TStrArray = array of String;
  6.  
  7. var
  8.   iArray: TIntArray;
  9.   sArray: TStrArray;
  10.   s: String;
  11.   i: Integer;
  12.  
  13. begin
  14.   SetLength(iArray,10);
  15.   iArray:=TIntArray.create(0,1,2,3,4,5,6,7,8,9);
  16.   for i in iArray do
  17.     case i of
  18.       9: WriteLn(i);
  19.       else Write(i,', ');
  20.     end;
  21.   WriteLn('setting iArray to Nil');
  22.   iArray:=Nil;
  23.   WriteLn('Length of iArray=',Length(iArray));
  24.  
  25.   SetLength(sArray,10);
  26.   sArray:=TStrArray.create('0','1','2','3','4','5','6','7','8','9');
  27.   for s in sArray do
  28.     case s of
  29.       '9': WriteLn(s);
  30.       else Write(s,', ');
  31.     end;
  32.   WriteLn('setting sArray to Nil');
  33.   sArray:=Nil;
  34.   WriteLn('Length of sArray=',Length(sArray));
  35.   ReadLn;
  36. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 4617
Re: Dynamic array cleanup
« Reply #7 on: August 27, 2017, 01:13:08 pm »
-glh
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

howardpc

  • Hero Member
  • *****
  • Posts: 2388
Re: Dynamic array cleanup
« Reply #8 on: August 27, 2017, 04:56:36 pm »
Initialization for dynamic arrays to Nil is documented here

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5804
Re: Dynamic array cleanup
« Reply #9 on: August 27, 2017, 07:08:34 pm »
-glh

So what ?
 
Heap dump by heaptrc unit
4 memory blocks allocated : 5192/5200
4 memory blocks freed     : 5192/5200
0 unfreed memory blocks : 0
True heap size : 360448 (80 used in System startup)
True free heap : 360368


bytebites

  • Full Member
  • ***
  • Posts: 118
Re: Dynamic array cleanup
« Reply #10 on: August 27, 2017, 07:58:01 pm »
foo is array of string and produced code is similar.
Code: Pascal  [Select]
  1. # [549] foo:=nil;
  2.         movl    $0,-120(%ebp)
  3.         leal    -120(%ebp),%eax
  4.         pushl   %eax
  5.         movl    $RTTI_$MAIN_$$_def00000216,%edx
  6.         leal    -76(%ebp),%eax
  7.         movl    $1,%ecx
  8.         call    fpc_dynarray_setlength
  9. .Ll76:
  10. # [550] SetLength(foo,0);
  11.         movl    $0,-120(%ebp)
  12.         leal    -120(%ebp),%eax
  13.         pushl   %eax
  14.         movl    $RTTI_$MAIN_$$_def00000216,%edx
  15.         leal    -76(%ebp),%eax
  16.         movl    $1,%ecx
  17.         call    fpc_dynarray_setlength

RAW

  • Hero Member
  • *****
  • Posts: 561
Re: Dynamic array cleanup
« Reply #11 on: August 28, 2017, 12:03:53 am »
 :D

Code: Pascal  [Select]
  1. // PRIVATE
  2.      arrStr: Array Of String;
  3.  
  4. Procedure TForm1.FormCreate(Sender: TObject);
  5.  Begin
  6.   SetLength(arrStr, 10);
  7.   ShowMessage(IntToStr(High(arrStr)));
  8.  End;
  9.  
  10. Procedure TForm1.FormClick(Sender: TObject);
  11.  Begin
  12.   Finalize(arrStr); // definitely optional !!!
  13.   ShowMessage(IntToStr(High(arrStr)));
  14.  End;
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC5 FPC 3.0.4

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus