[vtkusers] Deleting ImageData Inside Loop

David Doria daviddoria at gmail.com
Mon Jun 18 17:42:36 EDT 2012


On Mon, Jun 18, 2012 at 5:29 PM, matheus_viana <vianamp at gmail.com> wrote:

> Hi Guys,
>
> I've a code that looks like that:
>
> for (int i=100;i--;) {
>        vtkStructuredPointsReader *Reader =
> vtkStructuredPointsReader::New();
>        structured_dataset -> SetFileName("ImageData001.vtk");
>        structured_dataset -> Update();
>
>        vtkImageData *ImageData = vtkImageData::New();
>        ImageData = structured_dataset -> GetOutput();
>        ImageData -> Update();
>
>        /**
>                working on the ImageData
>        **/
>
>        ImageData -> Delete();
> }
>
> This code used to work well in VTK4, but in VTK5 it crashes at line
> ImageData -> Delete(); Commenting this line the code runs well, but the
> memory is not released at each interaction.
>
> Do you guys know how to solve that? Is one possibility use the class
> vtkSmartPointer? If so, how to do that?
>
> Thanks in advance,
> Matheus Viana
>

The memory for the ImageData is allocated inside of the reader, so you
should replace

       vtkImageData *ImageData = vtkImageData::New();
       ImageData = structured_dataset -> GetOutput();

with simply:

       vtkImageData *ImageData = structured_dataset -> GetOutput();

then you would not delete the imageData object, but rather the reader
object at the end of the loop. Alternatively (as you mentioned, you could
use

       vtkSmartPointer<vtkStructuredPointsReader> Reader
= vtkSmartPointer<vtkStructuredPointsReader>::New();

instead of

       vtkStructuredPointsReader *Reader =
vtkStructuredPointsReader::New();

and you wouldn't have to delete anything.

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120618/a0343fe9/attachment.htm>


More information about the vtkusers mailing list