[Insight-users] Error on buffer deletion

Frank Schmitt frank.schmitt at igd.fraunhofer.de
Sat Dec 17 07:14:21 EST 2005


Hi Jim,

i know what you mean, the code below was just an example. But i thought with
"itkimage->GetPixelContainer()->SetContainerManageMemory(false);"
i am telling the pixel-container not to destroy the buffer (and it does
work for all sorts of scalar images).

I am actually writing a computer-vision framework for students and i
wanted to make use of some itk-Filters. So what I do is something like the
following in a function:

1. I create an itk-image and set the buffer-pointer to the buffer of my
own image-class.
2. I create the itk-filter and set the input to the itk-image.
3. I set the output image-pixelbuffer to one of my own image-classes and
tell the itk-pixel-container not to destroy the buffer, since my
image-class should care about deletion.
4. The function goes out of scope (the smartpointers of the itk-images and
the itk-filter are deleted, and the pixel-buffer is not destroyed).

This all is working as expected, but if i delete my own image-class and i
used an itk-filter for rgb- or rgba-images i have a problem. No problem
with scalar-images (don't understand why?)

Thanks for your response,

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