I am studying the issue of competent memory management. As I understand it, it is not necessary to call the delete() method for each widget, it is enough to declare it parent when creating it and all parent widgets will be deleted when the parent is deleted.
Now, there is a QMdiSubWindow window. Window creation code:

 CatProducts *catProducts = new CatProducts(); catProducts->setObjectName("catProducts"); mdiArea->addSubWindow(catProducts); catProducts->show(); 

The Qt::WA_DeleteOnClose attribute is assigned to the Qt::WA_DeleteOnClose . On this window, I hang QWidget as an internal widget , and on it the rest of the widgets. Code like this:

 QWidget *internalWidget = new QWidget(this); setWidget(internalWidget); QGridLayout *layout = new QGridLayout(internalWidget); internalWidget->setLayout(layout); QTreeView *tree = new QTreeView; tree->setFixedWidth(100); layout->addWidget(tree,1, 1, 1, 3); QPushButton *button = new QPushButton("BUTTON"); layout->addWidget(button, 2, 1); 

Now the task manager in Windows is testing the memory consumption by opening / closing my QMdiSubWindow . When opening, the volume of consumed memory increases, and when closed, it does not decrease. Those. occupied memory is not released. How to do it right?

And another question: on the example of layout , but passing it as a parameter to the constructor of QTreeView *tree = new QTreeView(layout) and the layout->addWidget(tree) entry are identical? Those. in both cases, is the layout the parent widget for the tree ?

  • Many kinds of memory. Regarding operational (heap, which is LocalAlloc / VirtualAlloc) - it is always released. What kind of memory do you mean? - nick_n_a
  • A memory that is not released by itself is: shared memory, USER-heap memory, GDI-heap memory. Maybe I am missing something, but all the same, specify what memory you are losing. - nick_n_a
  • one
    It is not necessary to test the dispatcher of Windows, but with special programs for searching for leaks. Therefore, while there is no significant and constant increase in memory consumption with repeated actions, it is premature to talk about leaks. - αλεχολυτ
  • 1.If you do not use atypical memory accesses, then when you close the program, it will be released completely. The probability of leakage is almost 0. Memory leak is when after the 1000th start-up the system you will not move, slow down, and you have exhausted all the memory, you will not have 99.9% of this. 2. There is a "local leak" of memory during operation: A normal program will eat up memory and that's it. And if you are requesting more memory and forgetting, your program in the dispatcher will show more and more memory, until the notification “you have run out of memory” appears, but after closing it will free the memory. - nick_n_a
  • one
    Then I will answer this way: if you do not have the desire to learn all sorts of utilities, create a counter (int variable), or a counter procedure. Increase the counter for each new, decrease each delete. You can have one for all, you can be different - for different types of objects. Counters can 1) look at debager. 2) make a section - view counters. So you can analyze the behavior of the program. Counters can be wrapped in #ifdef _debug and #endif make it easier to remove. - nick_n_a

0