<div dir="ltr">Hi Brad,<div><br></div><div>Thanks for correcting me.</div><div><br></div><div>When I replace my AddImages() function with what's below, the memory for the<span class="inbox-inbox-Apple-converted-space"> </span><font face="monospace"><b>result</b></font><span class="inbox-inbox-Apple-converted-space"> </span>image still gets cleaned up automatically at the correct time.<br></div><div>Would you be able to give an example were the use of <font face="monospace"><b>auto</b></font> could lead to a memory leak? Since I guess I'm still not understanding the problem fully.</div><div><br></div><div>Thanks,</div><div>Kiran</div><div><br></div><div><div><font face="monospace">ImageType::Pointer AddImages(short a, short b) {</font></div><div><font face="monospace">    auto im1 = CreateImage(a);</font></div><div><font face="monospace">    auto im2 = CreateImage(b);</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    AddFilterType::Pointer adder = AddFilterType::New();</font></div><div><font face="monospace">    adder->SetInput1(im1);</font></div><div><font face="monospace">    adder->SetInput2(im2);</font></div><div><font face="monospace">    adder->Update();</font></div><div><font face="monospace">   <b> auto result = adder->GetOutput();</b></font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    AddFilterType::Pointer adder2 = AddFilterType::New();</font></div><div><font face="monospace">    adder2->SetInput1(im1);</font></div><div><font face="monospace">    adder2->SetInput2(result);</font></div><div><font face="monospace">    adder2->Update();</font></div><div><font face="monospace">    auto result2 = adder2->GetOutput();</font></div><div><span class="Apple-tab-span" style="white-space:pre"><font face="monospace">     </font></span></div><div><font face="monospace">    return result2;</font></div><div><font face="monospace">}</font></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, 21 Mar 2017 at 14:59 Lowekamp, Bradley (NIH/NLM/LHC) [C] <<a href="mailto:blowekamp@mail.nih.gov">blowekamp@mail.nih.gov</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



<div style="word-wrap:break-word" class="gmail_msg">
Hello,
<div class="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg">Your code and description illustrates the problem with the implicit auto type.</div></div><div style="word-wrap:break-word" class="gmail_msg">
<div class="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg">
<blockquote type="cite" class="gmail_msg">
<div dir="ltr" class="gmail_msg">
<div class="gmail_msg">Based on this simple test I think that it is probably safe to use <font face="monospace" class="gmail_msg"><b class="gmail_msg">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="gmail_msg"><br class="gmail_msg">
</div>
</div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">This can be problematic:</div></div><div style="word-wrap:break-word" class="gmail_msg">
<div class="gmail_msg">  auto testOutput = testFilter->GetOutput();</div>
<div class="gmail_msg"><br class="gmail_msg">
</div>
</div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">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="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg">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="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg">HTH,</div>
<div class="gmail_msg">Brad</div>
<div class="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg"><br class="gmail_msg">
<div class="gmail_msg">
<blockquote type="cite" class="gmail_msg"></blockquote></div></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg">
<div class="gmail_msg">On Mar 21, 2017, at 10:49 AM, Kiran Joshi <<a href="mailto:kiran.j88@gmail.com" class="gmail_msg" target="_blank">kiran.j88@gmail.com</a>> wrote:</div>
<br class="m_-7466972166993527894Apple-interchange-newline gmail_msg">
</blockquote></div></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div dir="ltr" class="gmail_msg">Hi Francois,
<div class="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg">Maybe I misunderstood, but I disagree with your statement that "you cannot use the keyword auto with these other methods"</div>
<div class="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg">Attached is a screenshot showing a small ITK test program in a Visual Studio debugging session. </div>
<div class="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg">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="gmail_msg">Note that I have used <font face="monospace" class="gmail_msg"><b class="gmail_msg">auto</b></font> when storing the image pointers returned by the CreateImage and AddImage functions.</div>
<div class="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg">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="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg">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="gmail_msg">On line 23 ~16MB of memory is used when the first image is created.</div>
<div class="gmail_msg">On line 24 ~32MB is used after the 2nd image is created.</div>
<div class="gmail_msg">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="gmail_msg">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="gmail_msg"><b class="gmail_msg">im1</b></font> and <font face="monospace" class="gmail_msg">
<b class="gmail_msg">im2</b></font> pointers have gone out of scope.</div>
<div class="gmail_msg">Finally on line 38 we exit the main() function and see that the memory usage returns to ~0MB, since the
<font face="monospace" class="gmail_msg"><b class="gmail_msg">image</b></font> variable also goes out of scope and the memory associated with it is deallocated.</div>
<div class="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg"><br class="gmail_msg">
</div>
<div class="gmail_msg">Let me know if you see any mistakes in my tests!</div>
<div class="gmail_msg">Kiran</div>
<div class="gmail_msg"><br class="gmail_msg">
</div>
</div></div></blockquote></div></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div dir="ltr" class="gmail_msg"><span id="m_-7466972166993527894cid:15af1551a065f63678a1" class="gmail_msg"><ITKPointerTest.png></span>
<div class="gmail_msg"><br class="gmail_msg">
</div>
<br class="gmail_msg">
<div class="gmail_quote gmail_msg">
<div dir="ltr" class="gmail_msg">On Tue, 21 Mar 2017 at 12:38 Francois Budin <<a href="mailto:francois.budin@kitware.com" class="gmail_msg" target="_blank">francois.budin@kitware.com</a>> wrote:<br class="gmail_msg">
</div>
<blockquote class="gmail_quote gmail_msg" 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_-7466972166993527894m_-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="gmail_msg"><br class="m_-7466972166993527894webkit-block-placeholder gmail_msg">
</div>
<div class="gmail_msg m_-7466972166993527894m_-3661160424353991923m_4393718566787835312markdown-here-exclude">
<div class="gmail_msg"><br class="m_-7466972166993527894webkit-block-placeholder gmail_msg">
</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="gmail_msg"><br class="m_-7466972166993527894webkit-block-placeholder gmail_msg">
</div>
</div>
<div style="margin:0px 0px 1.2em!important" class="gmail_msg"><br class="m_-7466972166993527894webkit-block-placeholder gmail_msg">
</div>
<div title="MDH:PHA+SGkgSGFucyw8L3A+PHA+VGhhbmtzIGZvciB5b3VyIGFuc3dlciAhPGJyPjwvcD48cD5JIHdhcyBhc2tpbmcgdGhhdCBiZWNhdXNlIHdoZW4gSSB1c2UgYXV0byBrZXl3b3JkIHdpdGggYSBmaWx0
ZXIgb3V0cHV0LCBpdCBzZWVtcyBsaWtlIGlzIHJlc3VsdCBpbiBhIG5vcm1hbCBwb2ludCB0eXBl
LCBub3QgYSBzbWFydCBwb2ludGVyLCBzbyBhIGNvZGUgd2l0aDwvcD5gYGA8YnI+YXV0byB0ZXN0
T3V0cHV0ID0gdGVzdEZpbHRlci0mZ3Q7R2V0T3V0cHV0KCk7PGJyPmBgYDxwPmhhcyBub3QgdGhl
IHNhbWUgYmVoYXZpb3IgYXM8L3A+PHA+YGBgPGJyPgpGaWx0ZXJPdXRwdXRUeXBlOjpQb2ludGVy
IHRlc3RPdXRwdXQgPSB0ZXN0RmlsdGVyLSZndDtHZXRPdXRwdXQoKTs8L3A+PHA+YGBgPC9wPjxw
PmB0ZXN0T3V0cHV0YCBpcyBhIHNtYXJ0IHBvaW50ZXIgaW4gdGhlIHNlY29uZCBjYXNlLCBhbmQg
YSBub3JtYWwgcG9pbnRlciBpbiB0aGUgZmlyc3Qgb25lLjxicj48L3A+PHA+SSB3YXMgd29uZGVy
aW5nIGlmIGl0IGNvdWxkIGludGVyZmVyZSB3aXRoIHRoZSBzbWFydCBwb2ludGVyIHJvbGUgKGZv
ciBleGFtcGxlIGNhdXNpbmcgbWVtb3J5IGxlYWssIG9yIHdyb25nIHJlZmVyZW5jZSBjb3VudCku
PC9wPjxwPkFmdGVyIGEgZGVlcGVyIGxvb2ssIGBOZXcoKWAgcmV0dXJuIGEgc21hcnQgcG9pbnRl
ciAoc28gd2l0aCBhdXRvID0sIG5vIHByb2JsZW0pIGFuZCBgR2V0T3V0cHV0KClgIGEgcG9pbnRl
ci4gU28gdGhlIHNlY29uZCBjb2RlIHNuaXBzZXQganVzdCBjcmVhdGVzIGFub3RoZXIgaW5zdGFu
Y2Ugb2YgYSBzbWFydCBwb2ludGVyLjxicj48L3A+SSBob3BlIG15IHF1ZXN0aW9uIGlzIGNsZWFy
ZXIgITxicj5ZYW5uPGJyPjxicj48ZGl2IGNsYXNzPSJtb3otY2l0ZS1wcmVmaXgiPkxlIDIxLzAz
LzIwMTcgw6AgMDE6NDMsIEpvaG5zb24sIEhhbnMgSiBhIMOpY3JpdCZuYnNwOzo8YnI+PC9kaXY+
PGJsb2NrcXVvdGUgY2l0ZT0ibWlkOjdEOTRGMDE2LTE4QTctNEY4OS05MzYxLTQ4NkFBNjU2MUU4
RUB1aW93YS5lZHUiIHR5cGU9ImNpdGUiPjxwcmUgd3JhcD0iIj5ZYW5uLAoKSXQgd2lsbCB3b3Jr
IG9ubHkgaWYgeW91IGFyZSBjb21waWxpbmcgd2l0aCBDKysxMSwgYW5kIHlvdSBoYXZlIG5vIGlu
dGVudGlvbiBvZiBzaGFyaW5nIHlvdXIgY29kZSB3aXRoIG90aGVycyB3aG8gbWF5IG5vdCB1c2Ug
QysrMTEgc3ludGF4LgoKSVRLIHByb3BlciBjdXJyZW50bHkgbWFpbnRhaW5zIGJhY2t3YXJkcyBj
b21wYXRpYmlsaXR5IHdpdGggb2xkZXIgdmVyc2lvbnMgb2YgQysrLgoKSGFucwoKCjwvcHJlPgoK
        PC9ibG9ja3F1b3RlPjxicj4=" style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding: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></div></blockquote></div></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">
_____________________________________<br class="gmail_msg">
Powered by <a href="http://www.kitware.com" 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" 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" 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" 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" class="gmail_msg" target="_blank">http://public.kitware.com/mailman/listinfo/insight-users</a><br class="gmail_msg"></div></blockquote></div></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">
_______________________________________________<br class="gmail_msg">
Community mailing list<br class="gmail_msg">
<a href="mailto:Community@itk.org" class="gmail_msg" target="_blank">Community@itk.org</a><br class="gmail_msg">
<a href="http://public.kitware.com/mailman/listinfo/community" class="gmail_msg" target="_blank">http://public.kitware.com/mailman/listinfo/community</a><br class="gmail_msg">
</div>
</blockquote>
</div>
<br class="gmail_msg">
</div>
</div>

</blockquote></div>