Memory Management Practices Stack vs Heap for Key Objects
-
Hello!
I have a question regarding memory management practices. In the code generated by Qt Creator for a new project:
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(u"qrc:/untitled/Main.qml"_qs); QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
I noticed that both QGuiApplication and QQmlApplicationEngine are declared on the stack. While the code functions perfectly, I'm wondering if there are potential advantages or disadvantages to storing these objects on the heap instead.
Furthermore, I'd like to understand if using the heap is considered good practice for longer-lived application classes in general.
Thank you
-
Hello!
I have a question regarding memory management practices. In the code generated by Qt Creator for a new project:
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(u"qrc:/untitled/Main.qml"_qs); QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
I noticed that both QGuiApplication and QQmlApplicationEngine are declared on the stack. While the code functions perfectly, I'm wondering if there are potential advantages or disadvantages to storing these objects on the heap instead.
Furthermore, I'd like to understand if using the heap is considered good practice for longer-lived application classes in general.
Thank you
@Drazz
Allocating these objects on the stack rather than the heap probably saves you, say, 32 bytes(!) and a few microseconds. Doing this saves you from having to remember to delete them when you no longer need them (if they are not auto-deleted in aQObject
hierarchy).Stack is fine if you want the object deleted when it goes out of scope; heap is required if they are to outlive their scope. Heap can get awkward if you are using
QObject
parentage relationships.Some people here seem to get excited about putting things on the stack in C++ if possible. But Qt runs just fine under Python/PySide where everything is heap not stack, so it can't be a requirement. So long as you get your lifetime/memory management right I don't think it's a great deal either way.
-
It really depends on the lifetime requirement of your Qt object.
I use the stack as much as possible for things such as modal QDialogs, QMessageBox etc. It's simple and there's no downside to it really when the object needs to exists only inside that one function where it's created.
-
Hello!
I have a question regarding memory management practices. In the code generated by Qt Creator for a new project:
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(u"qrc:/untitled/Main.qml"_qs); QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
I noticed that both QGuiApplication and QQmlApplicationEngine are declared on the stack. While the code functions perfectly, I'm wondering if there are potential advantages or disadvantages to storing these objects on the heap instead.
Furthermore, I'd like to understand if using the heap is considered good practice for longer-lived application classes in general.
Thank you
@Drazz Ultimately, it's not really a Qt specific question at all. More of a C++ style thing. Are you fairly new at C++ in-general?
If lexical scope (i.e., between two curly braces) matches lifetime, then you normally don't want to mess around with pointers and manual allocation. It's more work for no benefit. And you introduce the possibility that you forget to free it manually. Use the heap and explicit allocation when it's necessary. For example, if you need to create something in a function and have it live past the end of that function, you can't use the stack.
The QApplication exists for the scope of main(), so there's no good reason to overthink it.
-