[vtkusers] QVTKWidget2 memory leak?

Alex Malyushytskyy alexmalvtk at gmail.com
Tue Feb 28 18:14:52 EST 2012


The line below will crush the program. cause :widget will be deleted
twice, one time by parent ,
one time when going out of scope.

MyQVTKWidget2 widget(imageViewer);


You should never do such thing with Qt widget( and any QObject derived classes
Use new operator to instantiate children.

Alex



On Fri, Feb 24, 2012 at 2:25 PM, Jonathan Morra <jonmorra at gmail.com> wrote:
> OK, here is a small example that shows the problem.  I'm monitoring the
> memory using task manager.  When the render line is commented out the memory
> stays constant as you mouse over the image.  However, if that line is
> uncommented then then memory climbs rapidly.  The program crashes when you
> exit, but that doesn't concern me right now, I'm focused on the memory leak.
>
> Thanks
>
> #include <QtGui/QApplication>
>
> #include <QMainWindow>
>
> #include <QVTKWidget2.h>
>
> #include <vtkImageViewer2.h>
>
> #include <vtkImageData.h>
>
> #include <vtkPointData.h>
>
> #include <vtkGenericOpenGLRenderWindow.h>
>
> class MyQVTKWidget2 : public QVTKWidget2 {
>
> public:
>
>     MyQVTKWidget2 (vtkImageViewer2 *imageViewer, QWidget *parent = 0) :
> QVTKWidget2(parent) {
>
>         this->imageViewer = imageViewer;
>
>     }
>
>     virtual ~MyQVTKWidget2() {
>
>         imageViewer->Delete();
>
>     }
>
>     void mouseMoveEvent(QMouseEvent *) {
>
>         // Comment out this line to see the memory stay constant
>
>         imageViewer->Render();
>
>     }
>
> private:
>
>     vtkImageViewer2 *imageViewer;
>
> };
>
> int main(int argc, char *argv[]) {
>
>     QApplication a(argc, argv);
>
>     vtkImageData *imageData = vtkImageData::New();
>
>     imageData->SetExtent(0, 100, 0, 100, 0, 100);
>
>     imageData->SetOrigin(0, 0, 0);
>
>     imageData->SetSpacing(1, 1, 1);
>
>     imageData->AllocateScalars();
>
>     imageData->GetPointData()->GetScalars()->FillComponent(0, 0);
>
>     vtkImageViewer2 *imageViewer = vtkImageViewer2::New();
>
>     imageViewer->SetInput(imageData);
>
>     MyQVTKWidget2 widget(imageViewer);
>
>     imageViewer->SetRenderWindow(widget.GetRenderWindow());
>
>     QMainWindow mainWindow;
>
>     mainWindow.setGeometry(200, 200, 300, 300);
>
>     mainWindow.setCentralWidget(&widget);
>
>     mainWindow.show();
>
>     imageData->Delete();
>
>     return a.exec();
>
> }
>
>
> On Fri, Feb 24, 2012 at 1:36 PM, Jonathan Morra <jonmorra at gmail.com> wrote:
>>
>> I will spend some time and try and put together a small self contained
>> test.
>>
>>
>> On Fri, Feb 24, 2012 at 1:29 PM, Clinton Stimpson <clinton at elemtech.com>
>> wrote:
>>>
>>> On Friday, February 24, 2012 02:19:59 pm Jonathan Morra wrote:
>>> > I have recently converted my app from using QVTKWidget to QVTKWidget2
>>> > so
>>> > that I could paint on the QVTKWidget2 (which I was able to successfully
>>> > do).  After this conversion my app started to leak memory terribly.  I
>>> > followed the memory leak around, and found the culprit is the following
>>> > line
>>> >
>>> > glTexImage2D(GL_TEXTURE_2D, 0, internalFormat,
>>> >
>>> >                    xsize, ysize, 0, format,
>>> >
>>> >                    GL_UNSIGNED_BYTE, static_cast<const GLvoid
>>> > *>(data));
>>> >
>>> >
>>> > Which is in the following function
>>> >
>>> >
>>> > void vtkOpenGLImageSliceMapper::RenderTexturedPolygon(
>>> >
>>> >   vtkRenderer *ren, vtkImageProperty *property,
>>> >
>>> >   vtkImageData *input, int extent[6], bool recursive)
>>> >
>>> >
>>> > I am using a build that I got from git on January 10th.  I have a 64
>>> > bit Windows 7 machine, but have built VTK with Microsoft Visual
>>> > Studios 2008 32 bit.
>>> >
>>> >
>>> > This bug is manifested everytime I call render on a QVTKWidget2 that I
>>> > have placed a vtkImageViewer2 in.  This is a very critical bug for us
>>> > as now our program runs out of memory very quickly because there are
>>> > situations where render is called on every mouse move which means the
>>> > program will die extremely fast.  Any help would be appreciated.
>>> >
>>>
>>> Ok, I see the cause of the problem.
>>>
>>> QVTKWidget2::Start() calls OpenGLInit(), which resets the context
>>> creation
>>> time and the vtkOpenGLImageSliceMapper reloads them thinking its a new
>>> context.
>>>
>>> It would helpful if you can provide a minimal test case to demonstrate
>>> this.
>>>
>>> --
>>> Clinton Stimpson
>>> Elemental Technologies, Inc
>>> Computational Simulation Software, LLC
>>> www.csimsoft.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