I'm in trouble with Qt5's WA_DeleteOnClose
attribute.
This is the situation: I have a class M that extends QMainWindow
, and in this class I use an heap-allocated array. I read that with WA_DeleteOnClose
when the window M is closed, and the destructor called, every member with M as parent is deleted.
My question is: what's about the array? I know that every stack-allocated member is deallocated when the destructor is called, and i know that, in normal conditions, I have to call delete[] on my array. But in these conditions? If I don't use delete[] is my array deallocated because of the WA_DeleteOnClose
attribute?
Another question. Is the WA_DeleteOnClose attribute inherited by all M's childrens? In other words, do I have to set that attribute to true in all childrens?
Qt will just call your destructor, it doesn't go and delete every member of your class itself!
If you have a
delete[] myArray
in your class's destructor, your array will be deleted correctly.If you don't
delete[]
your array in the destructor, it will leak.But better yet, don't bother managing the memory yourself, this is poor style in C++11.
Use a
QVector
or astd::array
instead of a raw array in your class.