Sign in Join

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

Mailing lists / fpc-pascal / 2013-12

Memory leak in GetEnumerator

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Xiangrong Fang

Hi All,

I used HeapTrc on my TTreap class:

https://github.com/xrfang/fpcollection/blob/master/src/units/treap.pas

It reported memory leak in the following procedure:

function TTreap.GetEnumerator: TTreap;
begin
  Result := TTreap.Create;  //<-- memory leak here
  Result.ProxyFor := Self;
  ... ...
end;

Typical usage of the TTreap enumerator is:

 for n in aTreap do begin
   //do something with n
 end;

My question is: will the for-loop automatically free the enumerator after usage? How to fix leaks in my ttreap class?

Thanks a lot!

Xiangrong

Reply by email

Jeppe Græsdal Johansen
to Xiangrong Fang
Xiangrong Fang wrote 37 lines
Hi All, I used HeapTrc on my TTreap class: https://github.com/xrfang/fpcollection/blob/master/src/units/treap.pas It reported memory leak in the following procedure: function TTreap.GetEnumerator: TTreap; begin Result := TTreap.Create; //<-- memory leak here Result.ProxyFor := Self; ... ... end; Typical usage of the TTreap enumerator is: for n in aTreap do begin //do something with n end; My question is: will the for-loop automatically free the enumerator after usage? How to fix leaks in my ttreap class? Thanks a lot! Xiangrong

Your constructor allocates a new NullNode, which gets overwritten in GetEnumerator, and hence the enumerator NullNode is never freed.

Reply by email

Xiangrong Fang
to Jeppe Græsdal Johansen

You are right, the bug is fixed, and another bug in the GetEnumerator are also found and fixed.

This is my first time using heaptrc.

Thanks a lot.

2013/12/27 Jeppe Græsdal Johansen

Show quoted text 48 lines
Den 27-12-2013 10:16, Xiangrong Fang skrev: Hi All, I used HeapTrc on my TTreap class: https://github.com/xrfang/fpcollection/blob/master/src/units/treap.pas It reported memory leak in the following procedure: function TTreap.GetEnumerator: TTreap; begin Result := TTreap.Create; //<-- memory leak here Result.ProxyFor := Self; ... ... end; Typical usage of the TTreap enumerator is: for n in aTreap do begin //do something with n end; My question is: will the for-loop automatically free the enumerator after usage? How to fix leaks in my ttreap class? Thanks a lot! Xiangrong Your constructor allocates a new NullNode, which gets overwritten in GetEnumerator, and hence the enumerator NullNode is never freed. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email