<div dir="ltr">Hi Francois,<div><br></div><div>Maybe I misunderstood, but I disagree with your statement that "you cannot use the keyword auto with these other methods"</div><div><br></div><div>Attached is a screenshot showing a small ITK test program in a Visual Studio debugging session. </div><div><br></div><div>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>Note that I have used <font face="monospace"><b>auto</b></font> when storing the image pointers returned by the CreateImage and AddImage functions.</div><div><br></div><div>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><br></div><div>The first snapshot show that on line 22, very little memory has been allocated, since we have not yet created any images.</div><div>On line 23 ~16MB of memory is used when the first image is created.</div><div>On line 24 ~32MB is used after the 2nd image is created.</div><div>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>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"><b>im1</b></font> and <font face="monospace"><b>im2</b></font> pointers have gone out of scope.</div><div>Finally on line 38 we exit the main() function and see that the memory usage returns to ~0MB, since the <font face="monospace"><b>image</b></font> variable also goes out of scope and the memory associated with it is deallocated.</div><div><br></div><div>Based on this simple test I think that it is probably safe to use <font face="monospace"><b>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><br></div><div>Let me know if you see any mistakes in my tests!</div><div>Kiran</div><div><br></div><img src="cid:15af1551a065f63678a1" alt="ITKPointerTest.png" class="" style="max-width: 100%; opacity: 1;"><div><br></div><br><div class="gmail_quote"><div dir="ltr">On Tue, 21 Mar 2017 at 12:38 Francois Budin <<a href="mailto:francois.budin@kitware.com">francois.budin@kitware.com</a>> wrote:<br></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="m_-3661160424353991923m_4393718566787835312markdown-here-wrapper gmail_msg">
      <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>
      <p style="margin:0px 0px 1.2em!important" class="gmail_msg"></p>
      <div class="m_-3661160424353991923m_4393718566787835312markdown-here-exclude gmail_msg">
        <p class="gmail_msg"></p>
        <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>
        <p class="gmail_msg"></p>
      </div>
      <p style="margin:0px 0px 1.2em!important" class="gmail_msg"></p>
      <div title="MDH:PHA+SGkgSGFucyw8L3A+PHA+VGhhbmtzIGZvciB5b3VyIGFuc3dlciAhPGJyPjwvcD48cD5JIHdhcyBhc2tpbmcgdGhhdCBiZWNhdXNlIHdoZW4gSSB1c2UgYXV0byBrZXl3b3JkIHdpdGggYSBmaWx0
ZXIgb3V0cHV0LCBpdCBzZWVtcyBsaWtlIGlzIHJlc3VsdCBpbiBhIG5vcm1hbCBwb2ludCB0eXBl
LCBub3QgYSBzbWFydCBwb2ludGVyLCBzbyBhIGNvZGUgd2l0aDwvcD5gYGA8YnI+YXV0byB0ZXN0
T3V0cHV0ID0gdGVzdEZpbHRlci0mZ3Q7R2V0T3V0cHV0KCk7PGJyPmBgYDxwPmhhcyBub3QgdGhl
IHNhbWUgYmVoYXZpb3IgYXM8L3A+PHA+YGBgPGJyPgpGaWx0ZXJPdXRwdXRUeXBlOjpQb2ludGVy
IHRlc3RPdXRwdXQgPSB0ZXN0RmlsdGVyLSZndDtHZXRPdXRwdXQoKTs8L3A+PHA+YGBgPC9wPjxw
PmB0ZXN0T3V0cHV0YCBpcyBhIHNtYXJ0IHBvaW50ZXIgaW4gdGhlIHNlY29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HkgbWFpbnRhaW5zIGJhY2t3YXJkcyBj
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>