Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2008-03

TVarRec.VAnsiString memory leak?

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

Hi.

This construction of setup vr.VAnsiString cause memoryleak:

program str;
uses
  heaptrc;
var
  s: ansistring;
  vr: TVarRec;
begin
  SetString(s, 'xxx', 3); //ok
  vr.VType := vtAnsiString;
  SetString(AnsiString(vr.VAnsiString), 'yyy', 3); //Memory leak.
end.

Heap dump by heaptrc unit
2 memory blocks allocated : 24/32
1 memory blocks freed : 12/16
1 unfreed memory blocks : 12
True heap size : 32768
True free heap : 32672
Should be : 32688
Call trace for block $B7FEE0C0 size 12
$08050E82 $fpc_ansistr_setlength, line 569 of /home/common/fpc/rtl/inc/astrings.inc $080480DF main, line 10 of str.pas
$080678B1 _FPC_proc_start, line 67 of ./i386/si_prc.inc

Is right that in destroying TVarRec is not decremented ansistring reference? If so, how to correctly set TVarRec.VAnsiString without memoryleak? I need to fill "a: array of TVarRec" with ansistrings.

Thanks.

Petr

Ing. Petr Kristan . EPOS PRO s.r.o., Bozeny Nemcove 2625, 530 02 Pardubice tel: +420 466335223 Czech Republic (Eastern Europe) fax: +420 466510709

Reply by email

Michael Van Canneyt
to Unknown author
Show quoted text 15 lines
Hi. This construction of setup vr.VAnsiString cause memoryleak: program str; uses heaptrc; var s: ansistring; vr: TVarRec; begin SetString(s, 'xxx', 3); //ok vr.VType := vtAnsiString; SetString(AnsiString(vr.VAnsiString), 'yyy', 3); //Memory leak. end.

This is a correct way:

program str;
uses
  heaptrc;
var
  s,t: ansistring;
  vr: TVarRec;
begin
  SetString(s, 'xxx', 3); //ok
  vr.VType := vtAnsiString;
  t:='yyy';
  vr.VAnsiString:=Pointer(T);
end.

Michael.

Reply by email

Unknown author
to Michael Van Canneyt
Michael Van Canneyt wrote 18 lines
This is a correct way: program str; uses heaptrc; var s,t: ansistring; vr: TVarRec; begin SetString(s, 'xxx', 3); //ok vr.VType := vtAnsiString; t:='yyy'; vr.VAnsiString:=Pointer(T); end.

Final solution is not to use VAnsiString because reference counting little confusing. My problem was to fill "array of const" with strings. And these strings must be somewere stored.

Then i use

vr.VPChar = StrNew(PChar(s));
...
StrDispose(vr.VPChar);

Petr

Ing. Petr Kristan . EPOS PRO s.r.o., Bozeny Nemcove 2625, 530 02 Pardubice tel: +420 466335223 Czech Republic (Eastern Europe) fax: +420 466510709

Reply by email