Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Memory Management Practices Stack vs Heap for Key Objects
Forum Updated to NodeBB v4.3 + New Features

Memory Management Practices Stack vs Heap for Key Objects

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 650 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Drazz
    wrote on last edited by Drazz
    #1

    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

    JonBJ W 2 Replies Last reply
    0
    • D Drazz

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 a QObject 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.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SamiV123
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        1
        • D Drazz

          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

          W Offline
          W Offline
          wrosecrans
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          1
          • D Drazz has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved