views:

755

answers:

3

I am currently creating a program in Qt, OpenCv, Mac os X. I have a main window, and then a separate window that is opened. I pass the new window several matrix clones in the constructor:

ImageWindow *imageWin = new ImageWindow( 
   cvCloneMat(getData->getMasterRawMat(1)), 
   cvCloneMat(getData->getMasterRawMat(2)), 
   cvCloneMat(getData->getMasterRawMat(3)), 
   cvCloneMat(getData->getMasterRawMat(4)) );
imageWin->show();

How do I deallocate ( where do I call cvReleaseMat ), when the new window is closed?

+6  A: 

You can do that in e.g. your closeEvent(). Alternatively, if you use Qt::WA_DeleteOnClose for your widget attributes, the widget will be deleted when it is closed, which means you can place some clean-up routines in the destructor.

Ariya Hidayat
+1  A: 

Consider using RAII idiom and smart pointers and you won't need to remember when to release allocated memory.

Paul
Closing the window is != deleting the window. Smart pointer don't help here.
ypnos
In the current case if data is released in closeEvent() and window is reused later there will be UB when you try to access cloned matrices, because they are allocated in window constructor. Things that are allocated in constructor should be deleted in destructor in 99% of cases, including this.
Paul
All I wanted to say is that the question was about how to trigger destruction on window close. It was not about how to make destruction itself easier or smarter or whatever.
ypnos
I agree, my answer was not the direct answer to the specific question. But what I wanted to say is "Learn to manage data correctly and you won't need to ask such questions" :)
Paul
A: 

@Ariya Hidayat comment:

Make sure you properly parent your 'ImageWindow *imageWin'. Proper parenting important in QT for memory management :)

Karol Wilk