diff --git a/src/Core/Common/ReturnObjectFromFunction/Code.cxx b/src/Core/Common/ReturnObjectFromFunction/Code.cxx index 9db1493eb..ed9f0d5b0 100644 --- a/src/Core/Common/ReturnObjectFromFunction/Code.cxx +++ b/src/Core/Common/ReturnObjectFromFunction/Code.cxx @@ -57,26 +57,28 @@ main() std::cout << smartPointer->GetLargestPossibleRegion() << std::endl; } + // Common failure modes { - 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. + + // ImageType * pointer = ReturnPointer(); // std::cout << pointer->GetLargestPossibleRegion() << std::endl; - pointer = nullptr; // Here to silence warning } { - 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. + + // ImageType * pointer = ReturnSmartPointer(); // std::cout << pointer->GetLargestPossibleRegion() << std::endl; - pointer = nullptr; // Here to silence warning } { // 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; } diff --git a/src/Core/Mesh/AccessDataInCells/Code.cxx b/src/Core/Mesh/AccessDataInCells/Code.cxx index f513eafba..0335c4280 100644 --- a/src/Core/Mesh/AccessDataInCells/Code.cxx +++ b/src/Core/Mesh/AccessDataInCells/Code.cxx @@ -75,8 +75,8 @@ main(int, char *[]) while (cellDataIterator != end) { - PixelType cellValue = cellDataIterator.Value(); - // std::cout << cellValue << std::endl; //same values as before + const auto cellValue = cellDataIterator.Value(); + std::cout << cellValue << std::endl; // same values as before ++cellDataIterator; }