[vtkusers] Memory leaks

Alex Malyushytskyy alexmalvtk at gmail.com
Wed Dec 21 20:48:04 EST 2011


I have never used itk, but in this case it is unrelated -
you return pointer to the locally deleted object

 vtkImageData* imgWashedData_ =
itkConnectorItkToVtk_->GetImporter()->GetOutput();

// itkConnectorItkToVtk will be deleted when going out of scope
// ( functions returns )

// so you need to increase reference count
// how? exactly the same way you done with polydata

imgWashedData->Register(NULL); // adds reference count
imgWashedData->SetSource(NULL); // makes vtkDataObject to forget source

return imgWashedData_;

Try to read more about reference counting into vtk.

Alex

On Wed, Dec 21, 2011 at 11:29 AM, janavas <josenavasmolina at hotmail.com> wrote:
> Hi Alex!!
>
> Your solution works fine!! Thank you.
>
> Another quastion related to this topic is if I can do the same if it is a
> vtkImageData. Here's my code:
>
> typedef itk::ImageToVTKImageFilter<ImageType> ConnectorItkToVtkType;
> ConnectorItkToVtkType::Pointer itkConnectorItkToVtk_;
> vtkImageData* loadWashed3DImage(const std::string &filename, QStatusBar *
> /*statusBar*/)
> {
>        cout << "Loading " << filename << "..." << endl;
>
>        vtkTimerLog* timer = vtkTimerLog::New();
>        timer->StartTimer();
>
>        typedef itk::ImageFileReader< ImageType > ReaderType;
>        ReaderType::Pointer itkReader = ReaderType::New();
>
>        itkConnectorItkToVtk_ = ConnectorItkToVtkType::New();
>
>        typedef itk::FlipImageFilter< ImageType >  FilterType;
>        FilterType::Pointer flipper = FilterType::New();
>
>        typedef FilterType::FlipAxesArrayType FlipAxesArrayType;
>        FlipAxesArrayType flipArray;
>
>        flipArray[0] = false;
>        flipArray[1] = true;
>        flipArray[2] = false;
>
>        flipper->SetFlipAxes( flipArray );
>
>        typedef itk::ChangeInformationImageFilter< ImageType > ChangeInfoType;
>        ChangeInfoType::Pointer changeInfo = ChangeInfoType::New();
>        ImageType::PointType newOrigin;
>        newOrigin[0] = newOrigin[1] = newOrigin[2] = 0;
>        changeInfo->SetOutputOrigin( newOrigin );
>        changeInfo->ChangeOriginOn();
>
>        itkReader->SetFileName(filename.c_str());
>
>        flipper->SetInput( itkReader->GetOutput() );
>        changeInfo->SetInput( flipper->GetOutput() );
>        itkConnectorItkToVtk_->GetExporter()->SetInput( changeInfo->GetOutput() );
>        itkConnectorItkToVtk_->GetImporter()->Update();
>        try
>        {
>                itkReader->Update();
>        }
>        catch( itk::ExceptionObject & err )
>        {
>                cout << "ExceptionObject caught!\n" << err << endl;
>                return NULL;
>        }
>        vtkImageData* imgWashedData_ =
> itkConnectorItkToVtk_->GetImporter()->GetOutput();
>
>        timer->StopTimer();
>
>        cout << "Loaded.[ Time: " << timer->GetElapsedTime() << " seconds ]" <<
> endl;
>        timer->Delete();
>
>        return imgWashedData_;
> }
>
> I tried to do this (and making itkConnectorItkToVtk_ local):
>
> vtkImageData* imgWashedData_ =
> itkConnectorItkToVtk_->GetImporter()->GetOutput();
> imgWashedData_->Register(NULL);
> imgWashedData_->SetSource(NULL);
> timer->StopTimer();
>
> I didn't add the Delete() calls because itk is using itkSmartPointers and if
> I add this calls the application crash.
>
>
> But my app directly crash when I tried to do something with
> imgWashedData_...
>
> Thank you for your help
>
> Jose
>
> --
> View this message in context: http://vtk.1045678.n5.nabble.com/Memory-leaks-tp5037946p5092707.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers



More information about the vtkusers mailing list