[ITK] [ITK-users] auto keyword and New() smart pointer

Kiran Joshi kiran.j88 at gmail.com
Tue Mar 21 10:49:26 EDT 2017


Hi Francois,

Maybe I misunderstood, but I disagree with your statement that "you cannot
use the keyword auto with these other methods"

Attached is a screenshot showing a small ITK test program in a Visual
Studio debugging session.

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.
Note that I have used *auto* when storing the image pointers returned by
the CreateImage and AddImage functions.

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.

The first snapshot show that on line 22, very little memory has been
allocated, since we have not yet created any images.
On line 23 ~16MB of memory is used when the first image is created.
On line 24 ~32MB is used after the 2nd image is created.
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.
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
*im1* and *im2* pointers have gone out of scope.
Finally on line 38 we exit the main() function and see that the memory
usage returns to ~0MB, since the *image* variable also goes out of scope
and the memory associated with it is deallocated.

Based on this simple test I think that it is probably safe to use *auto* 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.

Let me know if you see any mistakes in my tests!
Kiran

[image: ITKPointerTest.png]


On Tue, 21 Mar 2017 at 12:38 Francois Budin <francois.budin at kitware.com>
wrote:

> Hello Yann,
>
> 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.
> The two following links give some insight in why ITK is implemented this
> way:
> https://itk.org/Wiki/ITK/Examples/Utilities/ReturnObjectFromFunction
> https://itk.org/pipermail/insight-developers/2011-April/018011.html
>
> Hope this helps,
> Francois
>
> On Tue, Mar 21, 2017 at 6:45 AM, asertyuio via Insight-users <
> insight-users at itk.org> wrote:
>
> Hi Hans,
>
> Thanks for your answer !
>
> 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
>
> auto testOutput = testFilter->GetOutput();
>
> has not the same behavior as
>
>  FilterOutputType::Pointer testOutput = testFilter->GetOutput();
>
> testOutput is a smart pointer in the second case, and a normal pointer in
> the first one.
>
> I was wondering if it could interfere with the smart pointer role (for
> example causing memory leak, or wrong reference count).
>
> After a deeper look, New() return a smart pointer (so with auto =, no
> problem) and GetOutput() a pointer. So the second code snipset just
> creates another instance of a smart pointer.
>
> I hope what was my question is now clearer !
> Yann
>
> Le 21/03/2017 à 01:43, Johnson, Hans J a écrit :
>
> 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
>
>
>
>>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/insight-users
>
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/insight-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20170321/d8407c67/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ITKPointerTest.png
Type: image/png
Size: 154512 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/community/attachments/20170321/d8407c67/attachment-0001.png>
-------------- next part --------------
_____________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users


More information about the Community mailing list