PDA

View Full Version : Why should a removeSubWindow() raise exception?



Raccoon29
20th March 2008, 12:34
Hi all,
my question tells for your very wide experience:
what are all the motivations for which a QMdiArea::removeSubWindow(QWidget* widget) should fail, raising an exception (and, so, crashing the program)?

Details:
MDI application.
QDialog as widget in a QMdiSubWindow.
When closing it, following rows are executed:


mdiArea->removeSubWindow(wArticle);
delete pArticle;
pArticle=NULL;
wArticle=NULL;

where:
mdiArea => QMdiArea*
wArticle => QMdiSubWindow*
pArticle => QDialog* (contained by wArticle)
The first of the four instructions raises an exception and makes crash the program.
So, what I'm asking is: what has wArticle to have of strange (since I checked it is not NULL) to make the method fail?

jpn
20th March 2008, 12:43
Either pointer could be a dangling pointer. You could have forgotten to initialize it or it could've been already deleted (and forgot to nullify).

Raccoon29
20th March 2008, 13:06
Either pointer could be a dangling pointer. You could have forgotten to initialize it or it could've been already deleted (and forgot to nullify).
Yes, I agree with you; maybe wArticle has been already deleted (probabily in a tricky way).

I read docs: so have I to delete wArticle after removing it from MdiArea, since it is not removed by removeSubWindow()? Is it right?

jpn
20th March 2008, 14:10
Usually QMdiSubWindows have Qt::WA_DeleteOnClose set which means they'll get deleted upon close event.

Raccoon29
20th March 2008, 14:23
Usually QMdiSubWindows have Qt::WA_DeleteOnClose set which means they'll get deleted upon close event.
Thank you, yes, now it is bright!

Update from the solving:
I tried to comment the row that raised the error (removeSubWindow()), and now everything works fine; I set the Qt::WA_DeleteOnClose so maybe when closing the window it got deleted, and when the method tried to remove it with the function, it found a deallocated memory: dump.
But now I'm wondering about the usefulness of the method removeSubWindow()... removing it, I don't risk memory lacks or something? The problem doesn't exist if what I just said is right, and DeleteOnClose works for it...

Anyway thank you very much,
you guru members make this forum unbeatable!

Raccoon29
21st March 2008, 09:42
I have another question that I think is related to this thread, so I didn't start a new one.

A Qt::WA_DeleteOnClose for a MdiSubWindow, when the subwindow get closed with event->accept() (in closeEvent()), deletes the QMdiSubWindow, or the QWidget inside it, or both of them??

jacek
21st March 2008, 10:49
A Qt::WA_DeleteOnClose for a MdiSubWindow, when the subwindow get closed with event->accept() (in closeEvent()), deletes the QMdiSubWindow, or the QWidget inside it, or both of them??
If that attribute was set for MDI subwindow, it will delete the subwindow and all child widgets.

Raccoon29
21st March 2008, 11:17
If that attribute was set for MDI subwindow, it will delete the subwindow and all child widgets.
Ok. Thank you jacek.