ITK/Examples/Utilities/ReturnObjectFromFunction: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Created page with "==ReturnObjectFromFunction.cxx== <source lang="cpp"> #include "itkImage.h" typedef itk::Image<unsigned char, 2> ImageType; ImageType::Pointer ReturnSmartPointer() { ImageType...")
 
(Deprecated content that is moved to sphinx)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
==ReturnObjectFromFunction.cxx==
{{warning|1=The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releasesIn many cases, the examples on this page no longer conform to the best practices for modern ITK versions.}}
<source lang="cpp">
#include "itkImage.h"
 
typedef itk::Image<unsigned char, 2> ImageType;
 
ImageType::Pointer ReturnSmartPointer()
{
  ImageType::Pointer image = ImageType::New();
  itk::Index<2> corner = {{0,0}};
  itk::Size<2> size = {{10,10}};
  itk::ImageRegion<2> region(corner, size);
  image->SetRegions(region);
  image->Allocate();
 
  return image;
}
 
ImageType* ReturnPointer()
{
  ImageType::Pointer image = ImageType::New();
  itk::Index<2> corner = {{0,0}};
  itk::Size<2> size = {{10,10}};
  itk::ImageRegion<2> region(corner, size);
  image->SetRegions(region);
  image->Allocate();
 
  return image;
}
 
int main(int argc, char *argv[])
{
  {
  // This is how it should be done.
  ImageType::Pointer smartPointer = ReturnSmartPointer();
  std::cout << smartPointer->GetLargestPossibleRegion() << std::endl;
  }
 
  {
  ImageType* pointer = ReturnPointer();
  // This crashes the program because the smart pointer created in the function goes out of scope and gets deleted
  // because it is returned as a normal pointer.
  // std::cout << pointer->GetLargestPossibleRegion() << std::endl;
  }
 
  {
  ImageType* pointer = ReturnSmartPointer();
  // This crashes the program because though the function returned a ::Pointer, it was not stored
  // anywhere so the reference count was not increased, so it got deleted.
  // std::cout << pointer->GetLargestPossibleRegion() << std::endl;
  }
 
  {
  // I thought this might also work, but it does not (crash).
   // My reasoning was that even though you don't return a smart pointer, you assign the object to a smart
  // pointer at return time, so it still has a reference count of 1.
  //ImageType::Pointer smartPointer = ReturnPointer(); // this line causes a 'glibc error'
  // std::cout << smartPointer->GetLargestPossibleRegion() << std::endl;
  }
 
  return 0;
}
 
</source>
 
{{ITKCMakeLists|ReturnObjectFromFunction}}

Latest revision as of 20:19, 6 June 2019

Warning: The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releases. In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.