<html>
<head>
<meta content="text/html; charset=windows-1251"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">Ping!<span class="transaction-comment"
data-sigil="transaction-comment" data-meta="0_1"><br>
</span><br>
Suggested is a wrapper over a raw pointer that is intended for
freeing wrapped memory at the end of wrappers lifetime if
ownership of a raw pointer was not taken away during the lifetime
of the wrapper. <br>
The main difference from unique_ptr is an ability to access the
wrapped pointer after the ownership is transferred.<br>
To make the ownership clearer the wrapper is designed for
local-scope usage only.<br>
<br>
The main goal of the wrapper is to eliminate leaks like those in <span
class="transaction-comment" data-sigil="transaction-comment"
data-meta="0_1">TGParser.cpp -</span><span
class="transaction-comment" data-sigil="transaction-comment"
data-meta="0_1"><span class="transaction-comment"
data-sigil="transaction-comment" data-meta="0_1"> </span><span
class="transaction-comment" data-sigil="transaction-comment"
data-meta="0_1"><a
href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/TGParser.cpp?r1=215176&r2=215175&pathrev=215176&diff_format=f">r215176</a></span>.
With the wrapper the fixes </span><span
class="transaction-comment" data-sigil="transaction-comment"
data-meta="0_1">applied at </span><span
class="transaction-comment" data-sigil="transaction-comment"
data-meta="0_1"><span class="transaction-comment"
data-sigil="transaction-comment" data-meta="0_1"><span
class="transaction-comment" data-sigil="transaction-comment"
data-meta="0_1"> </span><span class="transaction-comment"
data-sigil="transaction-comment" data-meta="0_1"><a
href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/TGParser.cpp?r1=215176&r2=215175&pathrev=215176&diff_format=f">r215176</a>
</span></span>could be refactored in the more easy and safe
way.</span><br>
<br>
Attached is a proposed interface/implementation.<br>
Any ideas, suggestions? Is it OK to move forward with the
solution?<br>
<br>
</div>
<blockquote cite="mid:54237A0E.4030504@Gmail.com" type="cite">
<meta http-equiv="content-type" content="text/html;
charset=windows-1251">
Hello everyone,<br>
<br>
I bring to discussion the necessity/design of a new type of smart
pointer. r215176 and r217791 rise the problem, <a
moz-do-not-send="true" href="http://reviews.llvm.org/D5443">D5443</a>
is devoted to the solution.<br>
r215176 applies several temporary ugly fixes of memory leaks in <span
class="transaction-comment" data-sigil="transaction-comment"
data-meta="0_1">TGParser.cpp </span>which would be great to be
refactored using smart pointers. <a moz-do-not-send="true"
href="http://reviews.llvm.org/D5443">D5443</a> demonstrates how
the solution with a certain type of smart pointer would look like
(see changes in <span class="transaction-comment"
data-sigil="transaction-comment" data-meta="0_1">TGParser::ParseDef(),
TGParser::InstantiateMulticlassDef() and
TGParser::ParseSimpleValue()</span>).<br>
<br>
Briefly:<br>
consider a leaky example:<br>
{<br>
T* p = new T;<br>
if (condition1) {<br>
f(p); // takes ownership of p<br>
}<br>
p->SomeMethod();<br>
<br>
if (condition2) {<br>
return nullptr; // Leak!<br>
}<br>
<br>
g(p); // don't take ownership of p<br>
return p;<br>
}<br>
<br>
The preferred solution would look like:<br>
{<br>
smart_ptr<T> p(new T);<br>
if (condition1) {<br>
f(p.StopOwn()); // takes ownership of p<br>
}<br>
p->SomeMethod();<br>
<br>
if (condition2) {<br>
return nullptr; // <br>
}<br>
<br>
g(p.Get()); // don't take ownership of p<br>
return p.StopOwn();<br>
}<br>
<br>
Neither unique_ptr nor shared_ptr can be used in the place of
smart_ptr as unique_ptr sets the raw pointer to nullptr after
release() (StopOwn() in the example above) whereas shared_ptr is
unable to release.<br>
<br>
Attached is a scratch that illustrates how the minimal
API/implementation of a desired smart pointer sufficient for
refactoring would look like. Any ideas and suggestions are
appreciated.<br>
<br>
<pre class="moz-signature" cols="72">--
Anton</pre>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">--
Anton</pre>
</body>
</html>