[fpc-pascal] FillChar/FillByte and Finalize()
    Olivier Sannier 
    olivier at obones.com
       
    Tue Mar 15 22:33:44 CET 2016
    
    
  
On 15/03/2016 22:14, Krzysztof wrote:
> Hi,
>
> Should I (and in which case) call Finalize() when 
> using FillChar/FillByte? Heaptrc unit is not detecting any memory 
> leak. For example:
>
> type
>   PMyRec = ^TMyRec;
>   TMyRec = record
>     Field1: Int64;
>     Field2: String;
>     Field3: Int64;
>     Field4: String;
>     Field5: Boolean;
>   end;
>
> procedure push(ARec: PMyRec);
> begin
>   writeln(ARec^.Field2,',', ARec^.Field4);
> end;
>
> procedure TForm1.Button1Click(Sender: TObject);
> begin
>   FillChar(f, SizeOf(TMyRec), 0);
>   f.Field1 := 1;
>   f.Field2 := 'abc';
>   push(@f);
>   FillChar(f, SizeOf(TMyRec), 0);
>   f.Field1 := 2;
>   f.Field2 := 'vbnj';
>   push(@f);
> end;
>
> Regards
>
Well, looking at your example, all strings are coming from constants in 
the source code, thus no reference counting takes place because no 
memory allocation was needed.
However, if you were to do something like this, I believe it would leak:
   f.Field1 := 1;
   f.Field2 := 'abc' + IntToStr(f.Field1);
This way, you are creating a true string instance, and overwriting the 
pointer with zeroes won't call Finalize.
    
    
More information about the fpc-pascal
mailing list