<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body text="#000000" bgcolor="#FFFFFF">
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
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
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>
</body>
</html>