[Insight-users] smart pointers and local scope
William A. Hoffman
billlist at nycap.rr.com
Wed Aug 18 10:38:02 EDT 2004
Windows memory management is different than UNIX.
With Windows, the memory is returned to the OS after delete or free,
so programs like top show more memory. With UNIX, the
memory is not returned to the OS, but is kept by the program until
exit. The memory is still being free'ed and is available to the
running program.
-Bill
At 10:12 AM 8/18/2004, Michael wrote:
>Hi,
>
>if I properly understand
>
>http://public.kitware.com/pipermail/insight-users/2003-September/004820.html
>
>any memory referenced by smart pointers should be released when leaving the local scope in which the smart pointers were defined. I've written the following little program:
>
>#include <iostream>
>#include "itkImage.h"
>#include "itkImageFileReader.h"
>
>int main(int argc, char** argv)
>{
> const unsigned int Dimension = 2;
> typedef unsigned char PixelType;
> typedef itk::Image<PixelType, Dimension> ImageType;
> typedef itk::ImageFileReader<ImageType> ReaderType;
>
> char ch;
>
> std::cout << "before block" << std::endl;
> std::cin >> ch;
>
>
> { // local scope
>
> ReaderType::Pointer reader = ReaderType::New();
>
> if (argc < 2) {
> std::cout << "filename required." << std::endl;
> return -1;
> }
> reader->SetFileName(argv[1]);
>
> try {
> reader->Update();
> } catch (itk::ExceptionObject &e) {
> std::cout << e << std::endl;
> return -1;
> }
>
> std::cout << "after update: " << std::endl;
> std::cin >> ch;
> }
>
> std::cout << "after block" << std::endl;
> std::cin >> ch;
>
> return 0;
>}
>
>When compiling it with Visual Studio 6 under Windows and looking at the memory usage at the points asking for input everything looks ok: The memory usage is about 1.5 MB before the local scope, about 7.8 MB after the update call and about 2.2 MB after leaving the local scope. Obviously, the image data has been released (I used the BrainProtonDensitySliceBorder20.png image, which I upscaled by a factor 10 (in each direction)). Except of the (small) difference of 0.7 MB the same amount of memory is used before and after the local scope (where do the 0.7 MB come from?).
>
>When I compile the program with gcc under SunOS, however, the memory usage behaves differently. "top" shows the following values:
>
>before block: SIZE 4888K, RES 1672K
>after update: SIZE 10M, RES 8520K
>after block: SIZE 10M, RES 8520K
>
>It seems to me, that the memory of the smart pointers was not freed after leaving the local scope. Why is the behaviour different from the one in Windows?
>
>Thanks,
>
>Michael
>
>
>_______________________________________________
>Insight-users mailing list
>Insight-users at itk.org
>http://www.itk.org/mailman/listinfo/insight-users
More information about the Insight-users
mailing list