<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
Hello,
<div class=""><br class="">
</div>
<div class="">Your code and description illustrates the problem with the implicit auto type.</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">
<blockquote type="cite" class="">
<div dir="ltr" class="">
<div class="">Based on this simple test I think that it is probably safe to use <font face="monospace" class=""><b class="">auto</b></font> to store the output from filters. It does not look like it interferes with the reference counting or leads to memory
 leaks, as Yann was worried about.</div>
</div>
</blockquote>
</div>
<div class=""><br class="">
</div>
<div class="">This can be problematic:</div>
<div class="">  auto testOutput = testFilter->GetOutput();</div>
<div class=""><br class="">
</div>
<div class="">But that is not what you are doing in your example. When you use the “auto” type you need to be careful to determine if the type is a raw pointer or a smart pointer. The GetOutput methods returns an raw pointer without reference counting. This
 is the type auto is above.</div>
<div class=""><br class="">
</div>
<div class="">However in your example the return type for your method is ImageType::Pointer, which is an ITK smart pointer. Your code adds an implicit cast for the raw pointer to ITK’s smart pointer. And this is not made clear by the usage of auto.</div>
<div class=""><br class="">
</div>
<div class="">HTH,</div>
<div class="">Brad</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
<div>
<blockquote type="cite" class="">
<div class="">On Mar 21, 2017, at 10:49 AM, Kiran Joshi <<a href="mailto:kiran.j88@gmail.com" class="">kiran.j88@gmail.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">Hi Francois,
<div class=""><br class="">
</div>
<div class="">Maybe I misunderstood, but I disagree with your statement that "you cannot use the keyword auto with these other methods"</div>
<div class=""><br class="">
</div>
<div class="">Attached is a screenshot showing a small ITK test program in a Visual Studio debugging session. </div>
<div class=""><br class="">
</div>
<div class="">The program is very simple; an AddImages() function is called, which itself calls a CreateImage() function twice before adding the created images and returning the result.</div>
<div class="">Note that I have used <font face="monospace" class=""><b class="">auto</b></font> when storing the image pointers returned by the CreateImage and AddImage functions.</div>
<div class=""><br class="">
</div>
<div class="">On the right of the image you can see snapshots of the memory usage as the program executes, and I have also annotated which lines of the program were executed immediately before taking the snapshot.</div>
<div class=""><br class="">
</div>
<div class="">The first snapshot show that on line 22, very little memory has been allocated, since we have not yet created any images.</div>
<div class="">On line 23 ~16MB of memory is used when the first image is created.</div>
<div class="">On line 24 ~32MB is used after the 2nd image is created.</div>
<div class="">On line 30 the two images are added together and the result is returned from the function. At this point ~48MB is used because all 3 image still exist in memory.</div>
<div class="">On line 31 we exit from the AddImages() function and can see that the memory for two temporary images has been correctly deallocated, since the
<font face="monospace" class=""><b class="">im1</b></font> and <font face="monospace" class="">
<b class="">im2</b></font> pointers have gone out of scope.</div>
<div class="">Finally on line 38 we exit the main() function and see that the memory usage returns to ~0MB, since the
<font face="monospace" class=""><b class="">image</b></font> variable also goes out of scope and the memory associated with it is deallocated.</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">Let me know if you see any mistakes in my tests!</div>
<div class="">Kiran</div>
<div class=""><br class="">
</div>
<span id="cid:15af1551a065f63678a1"><ITKPointerTest.png></span>
<div class=""><br class="">
</div>
<br class="">
<div class="gmail_quote">
<div dir="ltr" class="">On Tue, 21 Mar 2017 at 12:38 Francois Budin <<a href="mailto:francois.budin@kitware.com" class="">francois.budin@kitware.com</a>> wrote:<br class="">
</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr" class="gmail_msg">
<div class="gmail_msg">
<div class="gmail_msg">
<div class="gmail_msg">
<div class="gmail_msg">Hello Yann,<br class="gmail_msg">
<br class="gmail_msg">
</div>
Indeed, you are correct. New() will return a smart pointer, but other methods will return a pointer, so you cannot use the keyword "auto" with these other methods.<br class="gmail_msg">
</div>
The two following links give some insight in why ITK is implemented this way:<br class="gmail_msg">
<a href="https://itk.org/Wiki/ITK/Examples/Utilities/ReturnObjectFromFunction" class="gmail_msg" target="_blank">https://itk.org/Wiki/ITK/Examples/Utilities/ReturnObjectFromFunction</a><br class="gmail_msg">
<a href="https://itk.org/pipermail/insight-developers/2011-April/018011.html" class="gmail_msg" target="_blank">https://itk.org/pipermail/insight-developers/2011-April/018011.html</a><br class="gmail_msg">
<br class="gmail_msg">
</div>
Hope this helps,<br class="gmail_msg">
</div>
Francois<br class="gmail_msg">
</div>
<div class="gmail_extra gmail_msg"><br class="gmail_msg">
<div class="gmail_quote gmail_msg">On Tue, Mar 21, 2017 at 6:45 AM, asertyuio via Insight-users
<span dir="ltr" class="gmail_msg"><<a href="mailto:insight-users@itk.org" class="gmail_msg" target="_blank">insight-users@itk.org</a>></span> wrote:<br class="gmail_msg">
<blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF" class="gmail_msg">
<div class="gmail_msg m_-3661160424353991923m_4393718566787835312markdown-here-wrapper">
<p style="margin:0px 0px 1.2em!important" class="gmail_msg">Hi Hans,</p>
<p style="margin:0px 0px 1.2em!important" class="gmail_msg">Thanks for your answer !</p>
<p style="margin:0px 0px 1.2em!important" class="gmail_msg">I was asking that because when I use auto keyword with a filter output, it seems like is result in a normal point type, not a smart pointer, so a code with</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px" class="gmail_msg"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre-wrap;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important" class="gmail_msg">auto testOutput = testFilter->GetOutput();
</code></pre>
<p style="margin:0px 0px 1.2em!important" class="gmail_msg">has not the same behavior as</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px" class="gmail_msg"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre-wrap;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important" class="gmail_msg"> FilterOutputType::Pointer testOutput = testFilter->GetOutput();
</code></pre>
<p style="margin:0px 0px 1.2em!important" class="gmail_msg"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">testOutput</code>
 is a smart pointer in the second case, and a normal pointer in the first one.</p>
<p style="margin:0px 0px 1.2em!important" class="gmail_msg">I was wondering if it could interfere with the smart pointer role (for example causing memory leak, or wrong reference count).</p>
<p style="margin:0px 0px 1.2em!important" class="gmail_msg">After a deeper look, <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">
New()</code> return a smart pointer (so with auto =, no problem) and <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">
GetOutput()</code> a pointer. So the second code snipset just creates another instance of a smart pointer.</p>
<p style="margin:0px 0px 1.2em!important" class="gmail_msg">I hope what was my question is now clearer !<br class="gmail_msg">
Yann</p>
<span class="gmail_msg">
<p style="margin:0px 0px 1.2em!important" class="gmail_msg">Le 21/03/2017 à 01:43, Johnson, Hans J a écrit :</p>
<div style="margin: 0px 0px 1.2em !important;" class=""><br class="webkit-block-placeholder">
</div>
<div class="gmail_msg m_-3661160424353991923m_4393718566787835312markdown-here-exclude">
<div class=""><br class="webkit-block-placeholder">
</div>
<blockquote type="cite" class="gmail_msg">
<pre class="gmail_msg">Yann,

It will work only if you are compiling with C++11, and you have no intention of sharing your code with others who may not use C++11 syntax.

ITK proper currently maintains backwards compatibility with older versions of C++.

Hans


</pre>
</blockquote>
<div class=""><br class="webkit-block-placeholder">
</div>
</div>
<div style="margin: 0px 0px 1.2em !important;" class=""><br class="webkit-block-placeholder">
</div>
<div title="MDH:PHA+SGkgSGFucyw8L3A+PHA+VGhhbmtzIGZvciB5b3VyIGFuc3dlciAhPGJyPjwvcD48cD5JIHdhcyBhc2tpbmcgdGhhdCBiZWNhdXNlIHdoZW4gSSB1c2UgYXV0byBrZXl3b3JkIHdpdGggYSBmaWx0
ZXIgb3V0cHV0LCBpdCBzZWVtcyBsaWtlIGlzIHJlc3VsdCBpbiBhIG5vcm1hbCBwb2ludCB0eXBl
LCBub3QgYSBzbWFydCBwb2ludGVyLCBzbyBhIGNvZGUgd2l0aDwvcD5gYGA8YnI+YXV0byB0ZXN0
T3V0cHV0ID0gdGVzdEZpbHRlci0mZ3Q7R2V0T3V0cHV0KCk7PGJyPmBgYDxwPmhhcyBub3QgdGhl
IHNhbWUgYmVoYXZpb3IgYXM8L3A+PHA+YGBgPGJyPgpGaWx0ZXJPdXRwdXRUeXBlOjpQb2ludGVy
IHRlc3RPdXRwdXQgPSB0ZXN0RmlsdGVyLSZndDtHZXRPdXRwdXQoKTs8L3A+PHA+YGBgPC9wPjxw
PmB0ZXN0T3V0cHV0YCBpcyBhIHNtYXJ0IHBvaW50ZXIgaW4gdGhlIHNlY29uZCBjYXNlLCBhbmQg
YSBub3JtYWwgcG9pbnRlciBpbiB0aGUgZmlyc3Qgb25lLjxicj48L3A+PHA+SSB3YXMgd29uZGVy
aW5nIGlmIGl0IGNvdWxkIGludGVyZmVyZSB3aXRoIHRoZSBzbWFydCBwb2ludGVyIHJvbGUgKGZv
ciBleG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ding:0;margin:0" class="gmail_msg">
​</div>
</span></div>
</div>
<br class="gmail_msg">
_____________________________________<br class="gmail_msg">
Powered by <a href="http://www.kitware.com/" rel="noreferrer" class="gmail_msg" target="_blank">
www.kitware.com</a><br class="gmail_msg">
<br class="gmail_msg">
Visit other Kitware open-source projects at<br class="gmail_msg">
<a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" class="gmail_msg" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br class="gmail_msg">
<br class="gmail_msg">
Kitware offers ITK Training Courses, for more information visit:<br class="gmail_msg">
<a href="http://www.kitware.com/products/protraining.php" rel="noreferrer" class="gmail_msg" target="_blank">http://www.kitware.com/products/protraining.php</a><br class="gmail_msg">
<br class="gmail_msg">
Please keep messages on-topic and check the ITK FAQ at:<br class="gmail_msg">
<a href="http://www.itk.org/Wiki/ITK_FAQ" rel="noreferrer" class="gmail_msg" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br class="gmail_msg">
<br class="gmail_msg">
Follow this link to subscribe/unsubscribe:<br class="gmail_msg">
<a href="http://public.kitware.com/mailman/listinfo/insight-users" rel="noreferrer" class="gmail_msg" target="_blank">http://public.kitware.com/mailman/listinfo/insight-users</a><br class="gmail_msg">
<br class="gmail_msg">
</blockquote>
</div>
<br class="gmail_msg">
</div>
_____________________________________<br class="gmail_msg">
Powered by <a href="http://www.kitware.com/" rel="noreferrer" class="gmail_msg" target="_blank">
www.kitware.com</a><br class="gmail_msg">
<br class="gmail_msg">
Visit other Kitware open-source projects at<br class="gmail_msg">
<a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" class="gmail_msg" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br class="gmail_msg">
<br class="gmail_msg">
Kitware offers ITK Training Courses, for more information visit:<br class="gmail_msg">
<a href="http://www.kitware.com/products/protraining.php" rel="noreferrer" class="gmail_msg" target="_blank">http://www.kitware.com/products/protraining.php</a><br class="gmail_msg">
<br class="gmail_msg">
Please keep messages on-topic and check the ITK FAQ at:<br class="gmail_msg">
<a href="http://www.itk.org/Wiki/ITK_FAQ" rel="noreferrer" class="gmail_msg" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br class="gmail_msg">
<br class="gmail_msg">
Follow this link to subscribe/unsubscribe:<br class="gmail_msg">
<a href="http://public.kitware.com/mailman/listinfo/insight-users" rel="noreferrer" class="gmail_msg" target="_blank">http://public.kitware.com/mailman/listinfo/insight-users</a><br class="gmail_msg">
</blockquote>
</div>
</div>
_____________________________________<br class="">
Powered by <a href="http://www.kitware.com" class="">www.kitware.com</a><br class="">
<br class="">
Visit other Kitware open-source projects at<br class="">
<a href="http://www.kitware.com/opensource/opensource.html" class="">http://www.kitware.com/opensource/opensource.html</a><br class="">
<br class="">
Kitware offers ITK Training Courses, for more information visit:<br class="">
http://www.kitware.com/products/protraining.php<br class="">
<br class="">
Please keep messages on-topic and check the ITK FAQ at:<br class="">
http://www.itk.org/Wiki/ITK_FAQ<br class="">
<br class="">
Follow this link to subscribe/unsubscribe:<br class="">
http://public.kitware.com/mailman/listinfo/insight-users<br class="">
_______________________________________________<br class="">
Community mailing list<br class="">
Community@itk.org<br class="">
http://public.kitware.com/mailman/listinfo/community<br class="">
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>