[fpc-pascal] Memory leak in GetEnumerator

Jeppe Græsdal Johansen jeppe at j-software.dk
Fri Dec 27 11:48:05 CET 2013


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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20131227/c71f3e72/attachment.html>


More information about the fpc-pascal mailing list