E
E
Egorithm2017-02-02 11:27:44
Qt
Egorithm, 2017-02-02 11:27:44

Qt. Why is it necessary, and why is it not necessary to free memory?

I can't understand how it works in Qt.
56720ea727c14090b608ee1ed73e91aa.png
Is it necessary to create top-level objects (this) dynamically (after all, the order of creation and destruction can be different and then the stack cannot be used here)? If so, how is it that all lower-level objects release memory automatically? Should delete be used on the top-level object?
Example:
there is a class How to create a class Widget : public QWidget { };
top-level object
more correctly ? And how is the release of the entire hierarchy that is inherited from Base? Where is the destructor called to clear the memory? Not in Widget? Or does the MOC create its own part of the code to release?Widget Base;
Widget *Base = new Widget;
delete Base;
If so, what if I create an explicit destructor? Whether it is necessary to release storage for hierarchy now already most?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rou1997, 2017-02-02
@EgoRusMarch

What is the correct way to create a top-level object?:
so Widget Base;
or Widget *Base = new Widget;

Try to do the first, and then put this widget in a container or parent window, I refused this last summer, I don’t remember exactly why, but it seems like the widget should not appear, because for this it needs to go out of scope, and statically allocated variables when this are released.
In a container, that is, in a parent control or window - wherever you place a link to it.
Simply put, when the window is closed, the destructor from the central widget is called, which, in turn, is called from the child controls, etc., resulting in a final recursion.
It must be so.
Therefore, it should not be necessary, but if you conducted an experiment, looked at the source code and see that there is no such thing, then just implement all this manually when closing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question