[Insight-users] Error on buffer deletion

Miller, James V (Research) millerjv at crd.ge.com
Fri Dec 16 10:14:49 EST 2005


Frank, 

It is dangerous to go grab the pixel pointer and delete the memory.  There may be
other objects holding a reference to the pixel container and they will think that
data still exists.  ITK uses reference counting and smartpointers to manage the 
deallocation of data.  In your example, you manually deleted the buffer and when the 
itkImage went out of scope, it tried to delete the buffer a second time.

If you want to delete the pixel data, you should be able to just do

  reader->Update();
  ImageType::Pointer itkimage = reader->GetOutput();
  itkimage->ReleaseData();

This will force the image to have a new pixelcontainer.  If there are no
other references to the pixel container, then the bulk data will be deleted.

Wrt rgb images: if you read an rgb image into a scalar itkImage, then ITK will convert
the rgb data into luminance (a single scalar value per pixel).  If you read an rgb 
image into an rgb image, then you will have a three component image.  PNG images
can represent both scalar and rgb images.

Jim


-----Original Message-----
From: insight-users-bounces+millerjv=crd.ge.com at itk.org
[mailto:insight-users-bounces+millerjv=crd.ge.com at itk.org]On Behalf Of
Frank Schmitt
Sent: Friday, December 16, 2005 5:58 AM
To: insight-users at itk.org
Subject: [Insight-users] Error on buffer deletion


Hi,

if i want to delete the buffer of an rgb/rgba image an exception occurs
(this does not happen with scalar images).

Here is example code, which shows what i mean.
Is there something wrong with my code?

#include "itkRGBAPixel.h"
#include "itkRGBPixel.h"
#include "itkImage.h"
#include "itkImageFileReader.h"

int main(int argc, char ** argv)
{
  typedef itk::RGBAPixel< unsigned char >    PixelType;
// If you use the following PixelType no exception
//typedef unsigned char    PixelType;
  typedef itk::Image< PixelType, 2 >   ImageType;
  typedef itk::ImageFileReader< ImageType >  ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName( "someRGBImage.bmp" );
  reader->Update();
  ImageType::Pointer itkimage = reader->GetOutput();
  itkimage->GetPixelContainer()->SetContainerManageMemory(false);
  itkimage->SetGlobalReleaseDataFlag(false);
  PixelType* pixelbuffer = itkimage->GetBufferPointer();
  delete pixelbuffer;

  return 0;
}

Another thing I noticed is that after reading an rgb-image imageio says it
is scalar with 3 components, but reading an png-image everthing is as
expected: imageio says rgb image with 1 component?

Thanks for any help,
Frank




_______________________________________________
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