[vtkusers] Segfault when using vtkSmartPointer<vtkImageViewer2> in a QVTKWidget

Alex Malyushytskyy alexmalvtk at gmail.com
Wed Feb 29 20:45:22 EST 2012


There is certainly a bug. And it is mostly likely unrelated to vtkSmartPointer.
If you used the regular pointer instead, but called image_view->Delete()
right after tdisplay->show(); you should have similar problem as with
smart pointer.

I expect instance of vtkImageViewer2 is deleted when you are going out
of scope -
I do not think   ->SetRenderWindow(t); increases instance counter,
so when your call app.exec(); (nothing was shown yet, cause
application event loop did not start yet )

Why it is crushing is another question, but I do not think you wanted
such behaviour in the first place.

I would recommend sub-classing QVTKWidget and make  image_view a
member ( smart pointer will work )
to handle creation and deletion properly..

If you do not want  do this, you can use a regular pointer without
calling Delete() ,
most of the modern system will clean after your application, even
though technically you probably will have a memory leak.


Alex


On Tue, Feb 28, 2012 at 6:10 AM, t1m <t1m at phrogstar.de> wrote:
> Hello,
>
>  I have a problem with a vtkImageViewer2 instance in a QVTKWidget.  It looks
> like it has to do with the use of a vtkSmartPointer.
>
> To illustrate the problem, I wrote the following code:
>
> ----- main.cpp ------
> #include <qapplication.h>
>
> #include <QString>
> #include <QFileDialog>
> #include <QObject>
>
> #include <QVTKWidget.h>
>
> #include <vtkTIFFReader.h>
> #include <vtkImageViewer2.h>
> #include <vtkRenderWindow.h>
> #include <vtkSmartPointer.h>
> #include <vtkImageActor.h>
>
> int
> main(int argv, char ** argc)
> {
>    QApplication app(argv, argc);
>    QVTKWidget * tdisplay = new QVTKWidget();
>
>    //app.setMainWidget(tdisplay);
>    QString m_imageFileName =
> QFileDialog::getOpenFileName(tdisplay,QObject::tr("Open
> Image"),"",QObject::tr("*.tif *.tiff"));
>
>    std::cout << "m_imageFileName = " << m_imageFileName.toStdString() <<
> std::endl;
>
>    if(!m_imageFileName.isEmpty()) {
>        vtkSmartPointer<vtkTIFFReader> vtkReader =
> vtkSmartPointer<vtkTIFFReader>::New();
>        vtkReader->SetFileName(m_imageFileName.toStdString().c_str());
>        vtkReader->Update();
>
>        vtkSmartPointer<vtkImageViewer2> image_view =
> vtkSmartPointer<vtkImageViewer2>::New();
>        image_view->SetInput(vtkReader->GetOutput());
>
>        image_view->GetImageActor()->InterpolateOff();
>        image_view->SetColorLevel(127);
>        image_view->SetColorWindow(255);
>
>
> image_view->SetupInteractor(tdisplay->GetRenderWindow()->GetInteractor());
>        image_view->SetRenderWindow(tdisplay->GetRenderWindow());
>        tdisplay->show();
>    }
>
>    app.exec();
> }
> ----- end of main.cpp ------
>
> you can compile this with a short qt project file (change the INCLUDEPATH
> and LIBS accordingly):
>
> ----- testQVTK.pro -----
> TEMPLATE = app
> TARGET =
> DEPENDPATH += .
> INCLUDEPATH += .
>
> CONFIG += qt opengl debug
>
> # Input
> SOURCES += vtkTest.cpp
>
> INCLUDEPATH += /usr/local/include/vtk-5.8
>
> LIBS += -L/usr/local/lib/vtk-5.8 -lQVTK -lvtkRendering -lvtkCommon -lvtkIO
> -lvtkFiltering
> ----- end of testQVTK.pro -----
>
> qmake; make;
>
> fire up the programm, choose a tiff image and click into the window.  It
> crashes in both of my environments (Debian GNU/Linux amd64 and Mac OS X
> 10.6.8) with a segmentation fault.
>
> So I debugged it, and (most of the time) it crashes at this line in the
> function vtkImageViewer2Callback::Execute():
> 469           if (this->IV->GetInput() == NULL)
>
> The strange thing is, that IV indeed points to the Object allocated in the
> image_viewer vtkSmartPointer.  But examining it further, I could see that
> e.g. the RenderWindow pointer inside the vtkImageViewer2 pointer changes
> after the call to app.exec().
>
> So I tried a simple C++ pointer (vtkImageViewer2::New()) instead of the
> vtkSmartPointer of vtkImageViewer2, and the problem vanished!
>
> My question now is:  Did I do sth wrong with the use of vtkSmartPointer, or
> is this a bug?
>
> I appreciate any help,
>
>   cheers,
>
>     Tim
>
> --
> View this message in context: http://vtk.1045678.n5.nabble.com/Segfault-when-using-vtkSmartPointer-vtkImageViewer2-in-a-QVTKWidget-tp5521934p5521934.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