[fpc-pascal] string memory management
Sven Barth
pascaldragon at googlemail.com
Fri Apr 19 09:25:54 CEST 2013
Am 19.04.2013 06:36, schrieb Xiangrong Fang:
> Hi All,
>
> I'm studying how pascal manages strings. I wrote a simple test program:
>
> program stringtest;
> {$mode objfpc}{$H+}
> uses Classes, sysutils;
> function test: PString;
> var
> s : string;
> begin
> New(Result);
> Result^ := FloatToStr(Random);
> // s := FloatToStr(Random);
> // Result := @s;
> end;
> var
> i : Integer;
> begin
> Randomize;
> with TList.Create do try
> for i := 0 to 9 do Add(test);
> for i := 0 to Count - 1 do begin
> WriteLn(PString(Items[i])^);
> end;
> finally
> Free;
> end;
> end.
>
> The program runs fine, but:
>
> 1. I don't know whether I have to MANUALLY free memory for these
> strings to prevent leak?
The string types "String" (in mode $H+), "AnsiString" and
"UnicodeString" are managed by the compiler/RTL. If you use a pointer
type then you need to manage it yourself, thus you need to free the
PString manually in this case.
Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130419/a657923e/attachment.html>
More information about the fpc-pascal
mailing list