string memory management

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

string memory management

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?
2. Does the IDE provide any facility to analyze memory usage of a program and report if there are any leaks?  (There are Tools/Leak View, but I don't know how to get .trc file, or is it what I thought.

Thanks.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: string memory management

etrusco
On Fri, Apr 19, 2013 at 1:36 AM, Xiangrong Fang <[hidden email]> wrote:

> 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?
> 2. Does the IDE provide any facility to analyze memory usage of a program
> and report if there are any leaks?  (There are Tools/Leak View, but I don't
> know how to get .trc file, or is it what I thought.
>
> Thanks.

I think this can answer a few questions:
http://www.freepascal.org/docs-html/ref/refsu12.html#x35-380003.2.6

AFAICS your code will leak both the String contents and the PString pointer.
You have to store the result from test() and call Finalize or assign
'' to it (actually its "dereference").

> var
>   i : Integer;
     p: PString;
> begin
...
>     for i := 0 to Count - 1 do begin
>       WriteLn(PString(Items[i])^);
         p := PString(Items[i]);
         p^ := ''; // or Finalize(p^);
         Dispose(p);
>     end;

Best regards,
Flávio
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: string memory management

Michael Van Canneyt


On Fri, 19 Apr 2013, Flávio Etrusco wrote:

> On Fri, Apr 19, 2013 at 1:36 AM, Xiangrong Fang <[hidden email]> wrote:
>> 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?
Yes, if you work like this, you must do all that.
You must free all pointers in the list, and the strings they point to.

But why not use TStringList ? It does all this for you.

>> 2. Does the IDE provide any facility to analyze memory usage of a program
>> and report if there are any leaks?  (There are Tools/Leak View, but I don't
>> know how to get .trc file, or is it what I thought.

You can use the -gh compiler command-line option, then you'll get a listing of memory leaks.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: string memory management

Sven Barth-2
In reply to this post by Xiangrong Fang
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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: string memory management

Xiangrong Fang
In reply to this post by Michael Van Canneyt
2013/4/19 Michael Van Canneyt <[hidden email]>

1. I don't know whether I have to MANUALLY free memory for these strings to
prevent leak?

Yes, if you work like this, you must do all that. You must free all pointers in the list, and the strings they point to.

But why not use TStringList ? It does all this for you.

Because I want to then concat the strings into a big string using CRLF, but the TStringList.Text method concat them using LF only. I think that might be platform dependant, which is not what I want.

Now I use a TStringStream for this purpose.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: string memory management

Sven Barth-2
Am 19.04.2013 09:38, schrieb Xiangrong Fang:
2013/4/19 Michael Van Canneyt <[hidden email]>

1. I don't know whether I have to MANUALLY free memory for these strings to
prevent leak?

Yes, if you work like this, you must do all that. You must free all pointers in the list, and the strings they point to.

But why not use TStringList ? It does all this for you.

Because I want to then concat the strings into a big string using CRLF, but the TStringList.Text method concat them using LF only. I think that might be platform dependant, which is not what I want.
It seems that TextLineBreakStyle is what you want: http://www.freepascal.org/docs-html/rtl/classes/tstrings.textlinebreakstyle.html

Regards,
Sven

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal