<div dir="ltr"><div class="gmail_extra">Hi Bryan,</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 19, 2014 at 5:17 PM, Bryan Keiren <span dir="ltr"><<a href="mailto:bryan.keiren@guerrilla-games.com" target="_blank">bryan.keiren@guerrilla-games.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">





<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34);background-color:white">In the file \lib\Support\Process.cpp on line 60, it seems as though an unnecessary heap allocation and memory leak occurs.</span><span style="font-size:12pt;font-family:'Times New Roman',serif"><u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">This is the offending code:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u> <u></u></span></p>
<p class="MsoNormal"><i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">static TimeValue getElapsedWallTime() {</span></i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u><u></u></span></p>

<p class="MsoNormal"><i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">  static TimeValue &StartTime = *new TimeValue(TimeValue::now());</span></i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u><u></u></span></p>

<p class="MsoNormal"><i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">  return TimeValue::now() - StartTime;</span></i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u><u></u></span></p>

<p class="MsoNormal"><i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">}</span></i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">The issue is that the <i>StartTime</i> variable's value is allocated on the heap, after which a *<b>reference</b>* to it is stored (not the pointer itself). This
 means that the allocated memory is actually never properly de-allocated. </span></p></div></div></blockquote><div><br></div><div>What is the difference between storing a pointer and a reference here?</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple"><div><p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">Granted, the memory leak is small but if anyone attempts to write memory leak-free software while including LLVM code, this will be an issue (as it currently
 is for our company).<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">Is there anyone who knows why exactly this was implemented the way it currently is implemented? It seems rather unnecessary to be implemented with this complexity
 when a simpler implementation would work as well:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u> <u></u></span></p>
<p class="MsoNormal"><i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">static TimeValue getElapsedWallTime() {</span></i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u><u></u></span></p>

<p class="MsoNormal"><i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">  static TimeValue TimeValue(TimeValue::now());</span></i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u><u></u></span></p>

<p class="MsoNormal"><i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">  return TimeValue::now() - StartTime;</span></i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u><u></u></span></p>

<p class="MsoNormal"><i><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)">}</span></i></p></div></div></blockquote><div><br></div><div>Function-local static variables might be dangerous and lead to destruction-order problems. For example, if a program calls getElapsedWallTime() at destruction time (after main() exits), then static variable TimeValue might already</div>
<div>be destroyed/deallocated. Memory pointed to by StartValue will most likely be considered as "reachable" until the end of the program, and shouldn't cause error reports by leak detectors, such as LeakSanitizer, for instance (<a href="http://clang.llvm.org/docs/LeakSanitizer.html">http://clang.llvm.org/docs/LeakSanitizer.html</a>)</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><div>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Arial,sans-serif;color:rgb(34,34,34)"><u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>

<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>Alexey Samsonov, MSK</div>
</div></div>