<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">Den 27-12-2013 10:16, Xiangrong Fang
skrev:<br>
</div>
<blockquote
cite="mid:CAP93jB2MMtgKscY77hh4kcrrx93gvBpvtf-RBD0ftb1r9_OO_g@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_default" style="font-family:'courier
new',monospace">Hi All,</div>
<div class="gmail_default" style="font-family:'courier
new',monospace"><br>
</div>
<div class="gmail_default" style="font-family:'courier
new',monospace">
I used HeapTrc on my TTreap class:</div>
<div class="gmail_default" style="font-family:'courier
new',monospace"><br>
</div>
<div class="gmail_default" style="font-family:'courier
new',monospace"><a moz-do-not-send="true"
href="https://github.com/xrfang/fpcollection/blob/master/src/units/treap.pas">https://github.com/xrfang/fpcollection/blob/master/src/units/treap.pas</a><br>
</div>
<div class="gmail_default" style="font-family:'courier
new',monospace"><br>
</div>
<div class="gmail_default" style="font-family:'courier
new',monospace">It reported memory leak in the following
procedure:</div>
<div class="gmail_default" style="font-family:'courier
new',monospace">
<pre style="font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;margin-top:0px;margin-bottom:0px;color:rgb(51,51,51);line-height:18px">
<div class="" id="LC315" style="padding-left:10px"><span class="" style="font-weight:bold">
</span></div><div class="" id="LC315" style="padding-left:10px"><span class="" style="font-weight:bold">function</span> <span class="" style="color:rgb(68,85,136);font-weight:bold">TTreap</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(153,0,0);font-weight:bold">GetEnumerator</span><span class="" style="font-weight:bold">:</span> <span class="">TTreap</span><span class="" style="font-weight:bold">;</span></div>
<div class="" id="LC316" style="padding-left:10px"><span class="" style="font-weight:bold">begin</span></div><div class="" id="LC317" style="padding-left:10px"> <span class="" style="color:rgb(153,153,153)">Result</span> <span class="" style="font-weight:bold">:=</span> <span class="">TTreap</span><span class="" style="font-weight:bold">.</span><span class="">Create</span><span class="" style="font-weight:bold">; //<-- memory leak here</span></div>
<div class="" id="LC318" style="padding-left:10px"> <span class="" style="color:rgb(153,153,153)">Result</span><span class="" style="font-weight:bold">.</span><span class="">ProxyFor</span> <span class="" style="font-weight:bold">:=</span> <span class="" style="font-weight:bold">Self</span><span class="" style="font-weight:bold">;</span></div>
<div class="" id="LC318" style="padding-left:10px"><span class="" style="font-weight:bold"> ... ...</span></div><div class="" id="LC319" style="padding-left:10px"><span class="" style="font-size:12px;line-height:18px;font-weight:bold">end</span><span class="" style="font-size:12px;line-height:18px;font-weight:bold">;</span></div>
<div class="" id="LC319" style="padding-left:10px"><span class="" style="font-size:12px;line-height:18px;font-weight:bold">
</span></div></pre>
</div>
<div class="gmail_default" style="font-family:'courier
new',monospace">
Typical usage of the TTreap enumerator is:</div>
<div class="gmail_default" style="font-family:'courier
new',monospace"><br>
</div>
<div class="gmail_default" style="font-family:'courier
new',monospace">
<div class="gmail_default">
<pre style="font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;margin-top:0px;margin-bottom:0px;color:rgb(51,51,51);line-height:18px"><div class="" id="LC73" style="padding-left:10px"> <span class="" style="font-weight:bold">for</span> <span class="">n</span> <span class="" style="font-weight:bold">in</span> aTreap <span class="" style="font-weight:bold">do</span> <span class="" style="font-weight:bold">begin</span></div>
<div class="" id="LC74" style="padding-left:10px"> //do something with n</div><div class="" id="LC74" style="padding-left:10px"><span style="font-size:12px;line-height:18px"> </span><span class="" style="font-size:12px;line-height:18px;font-weight:bold">end</span><span class="" style="font-size:12px;line-height:18px;font-weight:bold">;</span></div>
<div><span class="" style="font-size:12px;line-height:18px;font-weight:bold">
</span></div></pre>
</div>
</div>
<div class="gmail_default" style="font-family:'courier
new',monospace">My question is: will the for-loop
automatically free the enumerator after usage? How to fix
leaks in my ttreap class?</div>
<div class="gmail_default" style="font-family:'courier
new',monospace"><br>
</div>
<div class="gmail_default" style="font-family:'courier
new',monospace">Thanks a lot!</div>
<div class="gmail_default" style="font-family:'courier
new',monospace">
<br>
</div>
<div class="gmail_default" style="font-family:'courier
new',monospace">Xiangrong</div>
<br>
</div>
</blockquote>
Your constructor allocates a new NullNode, which gets overwritten in
GetEnumerator, and hence the enumerator NullNode is never freed.<br>
<br>
</body>
</html>