@NRUB
I was just expecting that QObject will be destroyed while QApplication is finished
Why? Qt doesn't track all objects, it guarantees only that the parent will delete the child, nothing more. So if you don't give a QObject a parent it'll just float out there and no one will delete it automatically, thus it will leak.
is it possible to use QApplication instance as a parent for QObjects
It is, because QCoreApplication (and consequently all its derived classes) is already a QObject. That's one of the reasons it's also sometimes referred to as the "root QObject".
when my QWidget has no parent then it is alive as long as app is working and it is managed and deleted by app.
You can't give a QWidget instance a QObject parent, but you can connect the QCoreApplication::aboutToQuit() signal to the QObject::deleteLater() slot and this'd solve your leakage problems.
For example:
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
QWidget * window = new QWidget();
QObject::connect(&app, &QCoreApplication::aboutToQuit, window, &QObject::deleteLater);
window->show();
return QApplication::exec();
}
OS is removing leftovers when app is finishing
Which means that claimed memory will be released by the OS, but here the problem is that you actually need to have the heap allocated objects' destructors invoked, which the OS will not do.
which as we know works like a bridge between real addressing space and page addressing space which is translated by OS
Yes, we know that, but there are two issues with this. Firstly, there may not be any difference between physical and logical addressing and this is common when the OS is ran without paging; like I run my Linux without a swap partition. And secondly, paging has next to nothing in common with the problem here.
in practice memory is not leaking when apps are destroyed
Even if this were true, which it is for most OS-es, not taking care to clean up your resources leads to all kinds of funky stuff. For the most simple and obvious of examples - a driver holding a global resource is not deinitialized, and the global resource is left locked for the next program wanting to use it. Then what, should we restart the whole system?
Just leaving the OS to clean up, especially in such obvious circumstances, is in my opinion a lazy approach to programming that does more harm than good in the long run. Not only don't a bit of discipline and a handful of good practices hurt, on the contrary, they will spare you the trouble of digging through deep stack traces unnecessarily ...
Kind regards.