[vtkusers] Issues moving code to a function and returning a smart pointer

Manuel Corrales manuelcorrales at gmail.com
Fri May 17 15:43:38 EDT 2013


Hello all. I have a simple code that reads an image using ITK, then it
convert it to VTK and after that I display the image in a QVTKWidget. The
code is this and works fine:

 *    typedef itk::Image< double, 2 >         ImageType;  *
*      typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;    *
*      typedef itk::ImageFileReader<ImageType> ReaderType;*
*      *
*      ReaderType::Pointer reader = ReaderType::New();      *
*      reader->SetFileName(file.c_str());      *
*      reader->Update();*
*      *
*      ConnectorType::Pointer connector = ConnectorType::New();          *
*      connector->SetInput(reader->GetOutput());      *
*      connector->Update();*
*      ImageReader *myReader = new ImageReader();*
*      vtkSmartPointer<vtkImageViewer2> vtkImageViewer =
vtkSmartPointer<vtkImageViewer2>::New();      *
*      vtkImageViewer->SetInput(connector->GetOutput());      *
*
vtkImageViewer->SetRenderWindow(this->qvtkWidget->GetRenderWindow());      *
*      vtkImageViewer->Render();*


*Now if I change this code and create a separate function to perform the
connection from ITK to VTK, I get a segfault:*
*
*
*ImageReader *myReader = new ImageReader();*
*      vtkSmartPointer<vtkImageViewer2> vtkImageViewer =
myReader->renderITKReaderInVTK(file);**      *
*
vtkImageViewer->SetRenderWindow(this->qvtkWidget->GetRenderWindow());      *
*      vtkImageViewer->Render();*
*
*
and the called function:

*vtkSmartPointer<vtkImageViewer2>
ImageReader::renderITKReaderInVTK(std::string fileName) {*
*    typedef itk::Image< double, 2 >         ImageType;  *
*    typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;    *
*    typedef itk::ImageFileReader<ImageType> ReaderType;*
*    *
*    ReaderType::Pointer reader = ReaderType::New();*
*    qDebug() << "Setting reader file name";*
*    reader->SetFileName(fileName.c_str());    *
*    qDebug() << "Updating reader";*
*    reader->Update();*
*    *
*    ConnectorType::Pointer connector = ConnectorType::New();    *
*    qDebug() << "Setting input to connector";*
*    connector->SetInput(reader->GetOutput());    *
*    qDebug() << "Updating connector";*
*    connector->Update();*
*    *
*    vtkSmartPointer<vtkImageViewer2> vtkImageViewer =
vtkSmartPointer<vtkImageViewer2>::New();*
*    qDebug() << "Setting connector outupt -> imageViewer input";    *
*    vtkImageViewer->SetInput(connector->GetOutput());    *
*    qDebug() << "Image viewer set up ready";    *
*    return vtkImageViewer;*
*}*

Any ideas why the same code (I think) works if it is all together but fail
when it's in a different function?

Thanks in advance!
Regards,
Manuel.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130517/732b65e2/attachment.htm>


More information about the vtkusers mailing list