[ITK-users] [ITK] auto keyword and New() smart pointer
Kiran Joshi
kiran.j88 at gmail.com
Tue Mar 21 11:22:20 EDT 2017
Hi Brad,
Thanks for correcting me.
When I replace my AddImages() function with what's below, the memory for the
*result* image still gets cleaned up automatically at the correct time.
Would you be able to give an example were the use of *auto* could lead to a
memory leak? Since I guess I'm still not understanding the problem fully.
Thanks,
Kiran
ImageType::Pointer AddImages(short a, short b) {
auto im1 = CreateImage(a);
auto im2 = CreateImage(b);
AddFilterType::Pointer adder = AddFilterType::New();
adder->SetInput1(im1);
adder->SetInput2(im2);
adder->Update();
* auto result = adder->GetOutput();*
AddFilterType::Pointer adder2 = AddFilterType::New();
adder2->SetInput1(im1);
adder2->SetInput2(result);
adder2->Update();
auto result2 = adder2->GetOutput();
return result2;
}
On Tue, 21 Mar 2017 at 14:59 Lowekamp, Bradley (NIH/NLM/LHC) [C] <
blowekamp at mail.nih.gov> wrote:
> Hello,
>
> Your code and description illustrates the problem with the implicit auto
> type.
>
>
> 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.
>
>
> This can be problematic:
> auto testOutput = testFilter->GetOutput();
>
> 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.
>
> 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.
>
> HTH,
> Brad
>
>
> On Mar 21, 2017, at 10:49 AM, Kiran Joshi <kiran.j88 at gmail.com> wrote:
>
> 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.
>
>
> Let me know if you see any mistakes in my tests!
> Kiran
>
> <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
>
> _____________________________________
> 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
>
> _______________________________________________
> Community mailing list
> Community at itk.org
> http://public.kitware.com/mailman/listinfo/community
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20170321/8b8fcd7a/attachment.html>
More information about the Insight-users
mailing list