<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
07.05.2016, 20:10, Kurt Schwehr kirjoitti:<br>
<blockquote
cite="mid:CACmBxyui3pmUV=ADxsr5gqNw+6b5MaMVJsuqKkFSAwAtu2uzNA@mail.gmail.com"
type="cite">
<div dir="ltr">
<div>
<div class="gmail_extra">
<div class="gmail_quote"><br>
<div>This is cart before the horse but... as fast as I can
so expect typos. Now just think of a ~1K long function
or method with tons of instances and lots of places to
bailout successfully or as failures. We have > 9K
free/CPLFree/CPLdelete/CPLDestroys that could be <
~100.</div>
</div>
</div>
</div>
</div>
</blockquote>
<br>
I am definitely for having support for unique/smart pointers to
objects. In my experience one of the things making code difficult to
understand (and thus maintain/enhance) is long subroutines. Smart
pointers would often help a lot in that respect.<br>
<br>
Ari<br>
<br>
<blockquote
cite="mid:CACmBxyui3pmUV=ADxsr5gqNw+6b5MaMVJsuqKkFSAwAtu2uzNA@mail.gmail.com"
type="cite">
<div dir="ltr">
<div>
<div class="gmail_extra">
<div class="gmail_quote">
<div><br>
</div>
<div>GDALMyBigObj *poInstance =
CPLCalloc(sizeof(GDALMyBigObj);</div>
<div>...</div>
<div>
<div>if (oops) {</div>
<div> CPLDelete(poInstance)</div>
<div> return;</div>
<div>}<br>
</div>
</div>
<div>...</div>
<div>return; // kaboom</div>
<div>
<div>...</div>
<div>if (oops) {</div>
<div> CPLDelete(poInstance)</div>
<div> return;</div>
<div>}</div>
</div>
<div>...</div>
<div>
<div>if (oops) {</div>
<div> CPLDelete(poInstance)</div>
<div> return;</div>
<div>}</div>
</div>
<div><br>
</div>
<div>CPLDelete(poInstance)</div>
<div>return;</div>
<div><br>
</div>
<div>or worse</div>
<div><br>
</div>
<div>
<div>GDALMyBigObj *poInstance =
CPLCalloc(sizeof(GDALMyBigObj);</div>
<div>...</div>
<div>
<div>
<div>if (oops) {</div>
<div> goto END;<br>
</div>
<div>}<br>
</div>
</div>
<div>...</div>
</div>
<div>// Careful what you do here because you are
crossing gotos.</div>
<div>...</div>
<div>
<div>
<div>if (oops) {</div>
<div> goto END;<br>
</div>
<div>}<br>
</div>
</div>
<div>...</div>
</div>
<div>
<div>
<div>if (oops) {</div>
<div> goto END;<br>
</div>
<div>}<br>
</div>
</div>
<div>...</div>
</div>
<div>
<div>END:</div>
</div>
<div>CPLDelete(poInstance)</div>
<div>return;</div>
</div>
<div><br>
</div>
<div>when you could have... And yes, getting to this is
not trivial. There are multiple things here to discuss:</div>
<div><br>
</div>
<div>auto poInstance = <span
style="color:rgb(0,0,0);font-size:12.8px;line-height:1.2em;background-color:rgb(249,249,249)">std</span><span
class=""
style="font-size:12.8px;line-height:1.2em;color:rgb(0,128,128)">::</span><span
class=""
style="color:rgb(0,0,0);font-size:12.8px;line-height:1.2em">make_unique<GDALMyBigObj>(arg1,
arg2, arg3);</span></div>
<div><span class=""
style="color:rgb(0,0,0);font-size:12.8px;line-height:1.2em"><br>
</span></div>
<div><span class=""
style="color:rgb(0,0,0);font-size:12.8px;line-height:1.2em">if(oops)</span></div>
<div><span class=""
style="color:rgb(0,0,0);font-size:12.8px;line-height:1.2em">
return; // Everything cleaned up nice</span></div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px">...</span></font></div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px"><br>
</span></font></div>
<div>
<div><span class=""
style="color:rgb(0,0,0);font-size:12.8px;line-height:1.2em">if(oops)</span></div>
<div><span class=""
style="color:rgb(0,0,0);font-size:12.8px;line-height:1.2em">
return; // Everything cleaned up nice</span></div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px">...</span></font></div>
</div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px"><br>
</span></font></div>
<div>
<div><span class=""
style="color:rgb(0,0,0);font-size:12.8px;line-height:1.2em">if(oops)</span></div>
<div><span class=""
style="color:rgb(0,0,0);font-size:12.8px;line-height:1.2em">
return; // Everything cleaned up nice</span></div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px">...</span></font></div>
</div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px"><br>
</span></font></div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px">return;
// Woohoo! Success!</span></font></div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px"><br>
</span></font></div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px">My
example of the deleter comes from real code where I
don't what a heap checker barfing at me when ever a
test fails. Makes writing and debugging tests
insane....</span></font></div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px"><br>
</span></font></div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px">//
Apache 2.0 license...</span></font></div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px"><br>
</span></font></div>
<div><font color="#000000"><span
style="font-size:12.8px;line-height:15.36px">
<div style="">{</div>
<div style=""> // ...</div>
<div style=""> unique_ptr<OGRFeature>
feature(layer->GetNextFeature());</div>
<div style=""> OGRGeometry *geometry =
feature->GetGeometryRef();</div>
<div style=""> ASSERT_NE(nullptr, geometry);</div>
<div style=""><br>
</div>
<div style=""> char *wkt_tmp = nullptr;</div>
<div style=""> ASSERT_EQ(OGRERR_NONE,
geometry->exportToWkt(&wkt_tmp));</div>
<div style=""> std::unique_ptr<char,
CplFreeDeleter> wkt(wkt_tmp);</div>
<div style=""><br>
</div>
<div style=""> wkt_tmp = nullptr;</div>
<div style=""> ASSERT_STREQ("POINT (-109.27 10.3)",
wkt.get());</div>
<div style=""><br>
</div>
<div style="">}</div>
</span></font></div>
</div>
</div>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
gdal-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="http://lists.osgeo.org/mailman/listinfo/gdal-dev">http://lists.osgeo.org/mailman/listinfo/gdal-dev</a></pre>
</blockquote>
<br>
</body>
</html>