PDA

View Full Version : Deleting QMainWindow and QApplication Objects



mqt
14th May 2015, 14:49
I understand that the QWidget will delete its child widgets appropriately. What about the objects of QMainwindow, the super parent and QApplication. Do I need to delete them explicitly to clear the memory?

If it is an executable, (i.e. the QMainWindow and QApplication objects are created in main function), it may be fine because the entire application is going to exit anyway.

But what happens in case of a dll (i.e. QMainWindow and QApplication objects are created inside an exported dll-function)?
In this case, The dll-function is invoked from a parent application and later, QMainWindow is closed by the user. Remember that the parent application is still running. When the same session of parent application invokes the dll-function again, the Qt objects are created newly. So is there any chance of memory issues when it happens multiple time?

wysota
14th May 2015, 14:52
I understand that the QWidget will delete its child widgets appropriately. What about the objects of QMainwindow, the super parent and QApplication. Do I need to delete them explicitly to clear the memory?
The application object is usually created on the stack in main() so there is no need to delete it. As for main windows, they are widgets as any other so the same rules apply to it as to any other widget.

mqt
14th May 2015, 15:47
Thanks for the reply,

So who deletes the topmost parent widget?
And what about the second part of question? In case of DLL, they are not created on the stack in main().

wysota
14th May 2015, 15:49
So who deletes the topmost parent widget?
If it was allocated on stack then the compiler, if it was allocated on heap then the programmer :)


And what about the second part of question? In case of DLL, they are not created on the stack in main().

I don't know where and how you create the application object therefore I cannot answer your question. In general regular C++ rules apply here.

mqt
14th May 2015, 15:53
OK,
My only concern was this: if Qt also deletes these objects, it will be memory corruption. I know Qt deletes the memory for widgets. Just wanted to know if it deletes topmost parent widget and QApplication objects also. If Qt wont delete I can delete them. But I wanted to be sure that it is not double deleted. Thanks for the reply

wysota
14th May 2015, 16:02
There are two rules:

1. When an object is deleted, it deletes all its children
2. When object scope ends, it is deleted by the compiler

So there is no rule that "Qt deletes memory for widgets" or that it doesn't delete memory for the application object. All depends on the object scope and the scope of its parent (if present).

mqt
14th May 2015, 16:25
Those rules are for stack, right?
I am talking about the memory on heap which will not be deleted automatically when scope ends (QMainWindow *window = new QMainWIndow() ).
I was thinking, even for memory on heap, Qt deletes QWidget objects when its parent is closed.

wysota
14th May 2015, 17:06
Those rules are for stack, right?
No, those rules are for everything.


I am talking about the memory on heap which will not be deleted automatically when scope ends (QMainWindow *window = new QMainWIndow() ).
The scope of the pointer ends, not the object behind it. The pointer will be deleted, the object will not.

Lesiok
14th May 2015, 18:52
No, those rules are for everything.


The scope of the pointer ends, not the object behind it. The pointer will be deleted, the object will not.

Unless you use QScopedPointer.

wysota
14th May 2015, 23:46
Unless you use QScopedPointer.

A scoped pointer is an object, not a pointer :)

mqt
15th May 2015, 06:35
Thanks for all your comments. But still my doubt is not cleared.

I am clear about following
- In stack, the objects will get deleted when the scope ends
- In heap, we need to explicitly call "delete" or free to "clear" the memory
- When you declare a pointer, the pointer itself is in stack but the corresponding data is allocated in heap when we use "new" or "malloc"
- All above things are c/c++ concepts and nothing special about Qt
- In Qt, When we use QMainWindow *win=new QMainWindow(), the "win" is in stack and actual data pointed by "win" is in heap.
- If I create and add a widget to "win" by QWidget *childWin = new QWidget(win); actual data pointed by childWin is in heap. But I dont need to "delete" that explicitly because when "win" destructor is called, the "childWin" will be deleted. This is a Qt functionality.

But I am still not clear about following.
- As there is no parent for "win" (and QApplication *app), do I need to call "delete win" and "delete app" explicitly.

I am facing some memory corruption during the exit of my code. I am suspecting that I am doing double delete somewhere. Understanding what Qt does will help.

wysota
15th May 2015, 06:53
But I am still not clear about following.
- As there is no parent for "win" (and QApplication *app), do I need to call "delete win" and "delete app" explicitly.
The object is on a heap and has no parent to delete it. The conclusion seems clear.


I am facing some memory corruption during the exit of my code. I am suspecting that I am doing double delete somewhere. Understanding what Qt does will help.
Use Valgrind.

Lesiok
15th May 2015, 08:18
A scoped pointer is an object, not a pointer :)
Of course, but here we can say "it is a better pointer".

anda_skoa
16th May 2015, 11:02
- As there is no parent for "win" (and QApplication *app), do I need to call "delete win" and "delete app" explicitly.


For widgets you can, as an additional option, set the Qt::WA_DeleteOnClose attribute. Which delete the widget on close.

Cheers,
_