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. Pro and cons about creating QObjects components on the stack instead of creating them dynamically.

Pro and cons about creating QObjects components on the stack instead of creating them dynamically.

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 5 Posters 3.2k 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.
  • J Offline
    J Offline
    Jimmy Loyola
    wrote on 14 Apr 2020, 09:52 last edited by
    #1

    Are there any kind of problem if I allocate QObjects on the stack to avoid problem about memory management? Should I prefer dynamic allocated QObjects and delegate memory managament by qt object trees and its ownership system?

    J 1 Reply Last reply 14 Apr 2020, 09:55
    0
    • J Jimmy Loyola
      14 Apr 2020, 11:21

      @kshegunov Thank you, its a little bit unusual listening about that heap allocation is more secure than stack allocation for memory management. But if this is the qt philosophy we must follow it.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 14 Apr 2020, 11:28 last edited by kshegunov
      #11

      @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

      its a little bit unusual listening about that heap allocation is more secure than stack allocation for memory management. But if this is the qt philosophy we must follow it.

      I didn't say that, in fact I argued that both are valid approaches. As you can leak or double-delete a heap object, so you can double delete a stack object, it's just a fact of life. In any case Qt allows you both gracefully, so it's up to your preference which you're going to use. Me personally, I'm a stack-lover, I always put things in the stack if I can help it. It usually looks something like this:

      class Dummy : public QObject
      {
      public:
          using QObject::QObject;
      };
      
      class DummyParent : public QObject
      {
      public:
          DummyParent(QObject * parent = nullptr)
              : QObject(parent), owned(this)
          {
          }
      
          Dummy owned;
      };
      

      This construct is a FILO (i.e. stack) by itself, so it works gloriously without an issue.

      Read and abide by the Qt Code of Conduct

      J 2 Replies Last reply 14 Apr 2020, 11:59
      2
      • J Jimmy Loyola
        14 Apr 2020, 09:52

        Are there any kind of problem if I allocate QObjects on the stack to avoid problem about memory management? Should I prefer dynamic allocated QObjects and delegate memory managament by qt object trees and its ownership system?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 14 Apr 2020, 09:55 last edited by jsulm
        #2

        @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

        Are there any kind of problem if I allocate QObjects on the stack to avoid problem about memory management?

        Yes, you can cause double delete. Stack allocated objects are deleted as soon as they go out of scope. In case of QObject derived classes such object can have a parent. Parent deletes its children when it is deleted. But if the child was already deleted (because it went out of scope) parent would try to delete already deleted object. So, for QObject based objects it is better to stick with Qt object trees. But not always. For example it is perfectly fine to create a QFile object on the stack as long as you do not give it a parent.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        J K 2 Replies Last reply 14 Apr 2020, 10:12
        4
        • J jsulm
          14 Apr 2020, 09:55

          @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

          Are there any kind of problem if I allocate QObjects on the stack to avoid problem about memory management?

          Yes, you can cause double delete. Stack allocated objects are deleted as soon as they go out of scope. In case of QObject derived classes such object can have a parent. Parent deletes its children when it is deleted. But if the child was already deleted (because it went out of scope) parent would try to delete already deleted object. So, for QObject based objects it is better to stick with Qt object trees. But not always. For example it is perfectly fine to create a QFile object on the stack as long as you do not give it a parent.

          J Offline
          J Offline
          Jimmy Loyola
          wrote on 14 Apr 2020, 10:12 last edited by Jimmy Loyola
          #3

          @jsulm Thanks, which is the best approach in the qt environment to prevent leaks caused by a dynamic allocated qobject without ownership before it has assigned to a father.

          ...
          toolbar = new QToolBar; // has no ownership
          ...
          // something goes wrong and exit from the scope
          return; // leak caused by toolbar
          ...
          addToolBar(toolbar);
          ...
          
          J K 2 Replies Last reply 14 Apr 2020, 10:23
          0
          • J Jimmy Loyola
            14 Apr 2020, 10:12

            @jsulm Thanks, which is the best approach in the qt environment to prevent leaks caused by a dynamic allocated qobject without ownership before it has assigned to a father.

            ...
            toolbar = new QToolBar; // has no ownership
            ...
            // something goes wrong and exit from the scope
            return; // leak caused by toolbar
            ...
            addToolBar(toolbar);
            ...
            
            J Offline
            J Offline
            JonB
            wrote on 14 Apr 2020, 10:23 last edited by JonB
            #4

            @Jimmy-Loyola
            I'm going to throw my answer out there, ready to be shot down....

            If you really do feel you need to do this (automatically, without doing you own explicit delete), I would go:

            ...
            QWidget tempStackWidget;  // temporary QWidget on the function's stack
            toolbar = new QToolBar(&tempStackWidget); // now does have ownership, right from the start
            ...
            // something goes wrong and exit from the scope
            return; // tempStackWidget goes out of scope, taking new QToolbar with it
            ...
            addToolBar(toolbar); // new QToolbar moves from tempStackWidget to QMainWindow, so not destroyed
            ...
            
            1 Reply Last reply
            0
            • J jsulm
              14 Apr 2020, 09:55

              @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

              Are there any kind of problem if I allocate QObjects on the stack to avoid problem about memory management?

              Yes, you can cause double delete. Stack allocated objects are deleted as soon as they go out of scope. In case of QObject derived classes such object can have a parent. Parent deletes its children when it is deleted. But if the child was already deleted (because it went out of scope) parent would try to delete already deleted object. So, for QObject based objects it is better to stick with Qt object trees. But not always. For example it is perfectly fine to create a QFile object on the stack as long as you do not give it a parent.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 14 Apr 2020, 10:33 last edited by
              #5

              @jsulm said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

              @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

              Are there any kind of problem if I allocate QObjects on the stack to avoid problem about memory management?

              Yes, you can cause double delete. Stack allocated objects are deleted as soon as they go out of scope. In case of QObject derived classes such object can have a parent. Parent deletes its children when it is deleted.

              This is true.

              But if the child was already deleted (because it went out of scope) parent would try to delete already deleted object.

              This is incorrect. When the child goes out of scope it notifies the parent, so that's not the problematic case. The problematic case is when the parent goes out of scope before the child does. Then it's going to try to free the children and thus cause a stack corruption.

              So, for QObject based objects it is better to stick with Qt object trees.

              The stack and object trees aren't mutually exclusive.

              But not always. For example it is perfectly fine to create a QFile object on the stack as long as you do not give it a parent.

              Giving it a parent is fine as long as it doesn't outlive the parent.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              4
              • J Jimmy Loyola
                14 Apr 2020, 10:12

                @jsulm Thanks, which is the best approach in the qt environment to prevent leaks caused by a dynamic allocated qobject without ownership before it has assigned to a father.

                ...
                toolbar = new QToolBar; // has no ownership
                ...
                // something goes wrong and exit from the scope
                return; // leak caused by toolbar
                ...
                addToolBar(toolbar);
                ...
                
                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 14 Apr 2020, 10:39 last edited by kshegunov
                #6

                @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                @jsulm Thanks, which is the best approach in the qt environment to prevent leaks caused by a dynamic allocated qobject without ownership before it has assigned to a father.

                The best way is to declare the ownership explicitly. Assuming that code resides in a QWidget:

                toolbar = new QToolBar(this);
                if (wrong)  {
                    toolbar->deleteLater();
                    toolbar = nullptr;
                    return;
                }
                

                If this is in some outside class, you can do this:

                QScopedPointer<QToolBar> toolbar(new QToolBar());
                if (wrong)
                    return;
                
                widget->addToolBar(toolbar.take());
                

                Read and abide by the Qt Code of Conduct

                J J 2 Replies Last reply 14 Apr 2020, 11:05
                2
                • K kshegunov
                  14 Apr 2020, 10:39

                  @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                  @jsulm Thanks, which is the best approach in the qt environment to prevent leaks caused by a dynamic allocated qobject without ownership before it has assigned to a father.

                  The best way is to declare the ownership explicitly. Assuming that code resides in a QWidget:

                  toolbar = new QToolBar(this);
                  if (wrong)  {
                      toolbar->deleteLater();
                      toolbar = nullptr;
                      return;
                  }
                  

                  If this is in some outside class, you can do this:

                  QScopedPointer<QToolBar> toolbar(new QToolBar());
                  if (wrong)
                      return;
                  
                  widget->addToolBar(toolbar.take());
                  
                  J Offline
                  J Offline
                  JonB
                  wrote on 14 Apr 2020, 11:05 last edited by JonB
                  #7

                  @kshegunov
                  You haven't commented on my sugegstion, which probably means you don't like it but are too kind to me to say so ;-)

                  The question, as I read/think of it, is: Imagine I have a big function, with the new QToolbar at the top and lots of (potential) returns dotted all over the place, with the final addToolbar() near the end. I do not want to manually put in my own delete/deleteLater()s prior to the returns, because I'll make a mistake.

                  In that case, wouldn't my suggestion of a "temporary stack-based parent till the addToolbar() call" be a simple way of ensuring a newed QToolbar gets deleted on function scope exit be acceptable?

                  K J 2 Replies Last reply 14 Apr 2020, 11:09
                  1
                  • J JonB
                    14 Apr 2020, 11:05

                    @kshegunov
                    You haven't commented on my sugegstion, which probably means you don't like it but are too kind to me to say so ;-)

                    The question, as I read/think of it, is: Imagine I have a big function, with the new QToolbar at the top and lots of (potential) returns dotted all over the place, with the final addToolbar() near the end. I do not want to manually put in my own delete/deleteLater()s prior to the returns, because I'll make a mistake.

                    In that case, wouldn't my suggestion of a "temporary stack-based parent till the addToolbar() call" be a simple way of ensuring a newed QToolbar gets deleted on function scope exit be acceptable?

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 14 Apr 2020, 11:09 last edited by
                    #8

                    @JonB said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                    You haven't commented on my sugegstion, which probably means you don't like it but are too kind to me to say so ;-)

                    I don't like it, I'm saying so.

                    The question, as I read/think of it, is: Imagine I have a big function, with the new QToolbar at the top and lots of (potential) returns dotted all over the place. I do not want to manually put in my own delete/deleteLater()s prior to the returns, because I'll make a mistake.

                    Yes, that's a valid concern. Then you'd use a scoped pointer so you make sure you own that object all the way until you actually add it to a container widget (i.e. a toolbar in this case)

                    In that case, wouldn't my suggestion of a "temporary stack-based parent till the addToolbar() call" be a simple way of ensuring a newed QToolbar gets deleted on function scope exit be acceptable?

                    Yes, although I'd avoid creating the heavy (compared to an owing pointer) machinery that is a widget just to ensure an object is deleted, hence me not liking it - it's an abuse of notation so to speak. There are better and "lighter" ways to do it, as I've mentioned.

                    Read and abide by the Qt Code of Conduct

                    J 1 Reply Last reply 14 Apr 2020, 11:14
                    2
                    • K kshegunov
                      14 Apr 2020, 11:09

                      @JonB said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                      You haven't commented on my sugegstion, which probably means you don't like it but are too kind to me to say so ;-)

                      I don't like it, I'm saying so.

                      The question, as I read/think of it, is: Imagine I have a big function, with the new QToolbar at the top and lots of (potential) returns dotted all over the place. I do not want to manually put in my own delete/deleteLater()s prior to the returns, because I'll make a mistake.

                      Yes, that's a valid concern. Then you'd use a scoped pointer so you make sure you own that object all the way until you actually add it to a container widget (i.e. a toolbar in this case)

                      In that case, wouldn't my suggestion of a "temporary stack-based parent till the addToolbar() call" be a simple way of ensuring a newed QToolbar gets deleted on function scope exit be acceptable?

                      Yes, although I'd avoid creating the heavy (compared to an owing pointer) machinery that is a widget just to ensure an object is deleted, hence me not liking it - it's an abuse of notation so to speak. There are better and "lighter" ways to do it, as I've mentioned.

                      J Offline
                      J Offline
                      JonB
                      wrote on 14 Apr 2020, 11:14 last edited by
                      #9

                      @kshegunov Good stuff, point taken, thanks for letting me down gently ;-)

                      1 Reply Last reply
                      3
                      • K kshegunov
                        14 Apr 2020, 10:39

                        @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                        @jsulm Thanks, which is the best approach in the qt environment to prevent leaks caused by a dynamic allocated qobject without ownership before it has assigned to a father.

                        The best way is to declare the ownership explicitly. Assuming that code resides in a QWidget:

                        toolbar = new QToolBar(this);
                        if (wrong)  {
                            toolbar->deleteLater();
                            toolbar = nullptr;
                            return;
                        }
                        

                        If this is in some outside class, you can do this:

                        QScopedPointer<QToolBar> toolbar(new QToolBar());
                        if (wrong)
                            return;
                        
                        widget->addToolBar(toolbar.take());
                        
                        J Offline
                        J Offline
                        Jimmy Loyola
                        wrote on 14 Apr 2020, 11:21 last edited by
                        #10

                        @kshegunov Thank you, its a little bit unusual listening about that heap allocation is more secure than stack allocation for memory management. But if this is the qt philosophy we must follow it.

                        K 1 Reply Last reply 14 Apr 2020, 11:28
                        0
                        • J Jimmy Loyola
                          14 Apr 2020, 11:21

                          @kshegunov Thank you, its a little bit unusual listening about that heap allocation is more secure than stack allocation for memory management. But if this is the qt philosophy we must follow it.

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 14 Apr 2020, 11:28 last edited by kshegunov
                          #11

                          @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                          its a little bit unusual listening about that heap allocation is more secure than stack allocation for memory management. But if this is the qt philosophy we must follow it.

                          I didn't say that, in fact I argued that both are valid approaches. As you can leak or double-delete a heap object, so you can double delete a stack object, it's just a fact of life. In any case Qt allows you both gracefully, so it's up to your preference which you're going to use. Me personally, I'm a stack-lover, I always put things in the stack if I can help it. It usually looks something like this:

                          class Dummy : public QObject
                          {
                          public:
                              using QObject::QObject;
                          };
                          
                          class DummyParent : public QObject
                          {
                          public:
                              DummyParent(QObject * parent = nullptr)
                                  : QObject(parent), owned(this)
                              {
                              }
                          
                              Dummy owned;
                          };
                          

                          This construct is a FILO (i.e. stack) by itself, so it works gloriously without an issue.

                          Read and abide by the Qt Code of Conduct

                          J 2 Replies Last reply 14 Apr 2020, 11:59
                          2
                          • J JonB
                            14 Apr 2020, 11:05

                            @kshegunov
                            You haven't commented on my sugegstion, which probably means you don't like it but are too kind to me to say so ;-)

                            The question, as I read/think of it, is: Imagine I have a big function, with the new QToolbar at the top and lots of (potential) returns dotted all over the place, with the final addToolbar() near the end. I do not want to manually put in my own delete/deleteLater()s prior to the returns, because I'll make a mistake.

                            In that case, wouldn't my suggestion of a "temporary stack-based parent till the addToolbar() call" be a simple way of ensuring a newed QToolbar gets deleted on function scope exit be acceptable?

                            J Offline
                            J Offline
                            Jimmy Loyola
                            wrote on 14 Apr 2020, 11:32 last edited by
                            #12

                            @JonB I liked your answer too much. I forgot to say that in some cases I could have objects that couldn't have a parent like a toolbar or other qt starndard objects. In that case I can't give them a temporary parent.

                            1 Reply Last reply
                            0
                            • K kshegunov
                              14 Apr 2020, 11:28

                              @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                              its a little bit unusual listening about that heap allocation is more secure than stack allocation for memory management. But if this is the qt philosophy we must follow it.

                              I didn't say that, in fact I argued that both are valid approaches. As you can leak or double-delete a heap object, so you can double delete a stack object, it's just a fact of life. In any case Qt allows you both gracefully, so it's up to your preference which you're going to use. Me personally, I'm a stack-lover, I always put things in the stack if I can help it. It usually looks something like this:

                              class Dummy : public QObject
                              {
                              public:
                                  using QObject::QObject;
                              };
                              
                              class DummyParent : public QObject
                              {
                              public:
                                  DummyParent(QObject * parent = nullptr)
                                      : QObject(parent), owned(this)
                                  {
                                  }
                              
                                  Dummy owned;
                              };
                              

                              This construct is a FILO (i.e. stack) by itself, so it works gloriously without an issue.

                              J Offline
                              J Offline
                              Jimmy Loyola
                              wrote on 14 Apr 2020, 11:59 last edited by
                              #13

                              @kshegunov Doesn't Dummy class have a constructor?

                              1 Reply Last reply
                              0
                              • K kshegunov
                                14 Apr 2020, 11:28

                                @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                                its a little bit unusual listening about that heap allocation is more secure than stack allocation for memory management. But if this is the qt philosophy we must follow it.

                                I didn't say that, in fact I argued that both are valid approaches. As you can leak or double-delete a heap object, so you can double delete a stack object, it's just a fact of life. In any case Qt allows you both gracefully, so it's up to your preference which you're going to use. Me personally, I'm a stack-lover, I always put things in the stack if I can help it. It usually looks something like this:

                                class Dummy : public QObject
                                {
                                public:
                                    using QObject::QObject;
                                };
                                
                                class DummyParent : public QObject
                                {
                                public:
                                    DummyParent(QObject * parent = nullptr)
                                        : QObject(parent), owned(this)
                                    {
                                    }
                                
                                    Dummy owned;
                                };
                                

                                This construct is a FILO (i.e. stack) by itself, so it works gloriously without an issue.

                                J Offline
                                J Offline
                                Jimmy Loyola
                                wrote on 14 Apr 2020, 12:10 last edited by
                                #14

                                @kshegunov I'm a stack-lover too so I'am traying to learn how to manage qt stacked objects properly. Could you link some good documentation about this topic? I would be glad. I see even qt recommend use dynamic memory to allocate gui objects but its docs doesn't specify how to deal with potential problems of this approach in a detailed way.

                                K 1 Reply Last reply 14 Apr 2020, 13:19
                                0
                                • J Jimmy Loyola
                                  14 Apr 2020, 12:10

                                  @kshegunov I'm a stack-lover too so I'am traying to learn how to manage qt stacked objects properly. Could you link some good documentation about this topic? I would be glad. I see even qt recommend use dynamic memory to allocate gui objects but its docs doesn't specify how to deal with potential problems of this approach in a detailed way.

                                  K Offline
                                  K Offline
                                  kshegunov
                                  Moderators
                                  wrote on 14 Apr 2020, 13:19 last edited by
                                  #15

                                  @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                                  I'm a stack-lover too so I'am traying to learn how to manage qt stacked objects properly. Could you link some good documentation about this topic?

                                  Not currently, but I'll try to muster something this evening. In the mean time the major rule is - make sure the child is freed (by the stack) before the parent, this is enough. Also be wary of ownership transfer in Qt (it's noted in the docs).

                                  I would be glad. I see even qt recommend use dynamic memory to allocate gui objects but its docs doesn't specify how to deal with potential problems of this approach in a detailed way.

                                  I wouldn't go as far as to say it "recommends" it, but yes, it's more usual.

                                  Read and abide by the Qt Code of Conduct

                                  J 1 Reply Last reply 14 Apr 2020, 15:17
                                  1
                                  • K kshegunov
                                    14 Apr 2020, 13:19

                                    @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                                    I'm a stack-lover too so I'am traying to learn how to manage qt stacked objects properly. Could you link some good documentation about this topic?

                                    Not currently, but I'll try to muster something this evening. In the mean time the major rule is - make sure the child is freed (by the stack) before the parent, this is enough. Also be wary of ownership transfer in Qt (it's noted in the docs).

                                    I would be glad. I see even qt recommend use dynamic memory to allocate gui objects but its docs doesn't specify how to deal with potential problems of this approach in a detailed way.

                                    I wouldn't go as far as to say it "recommends" it, but yes, it's more usual.

                                    J Offline
                                    J Offline
                                    Jimmy Loyola
                                    wrote on 14 Apr 2020, 15:17 last edited by Jimmy Loyola
                                    #16

                                    @kshegunov said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                                    make sure the child is freed (by the stack) before the parent

                                    What do you thinkg about make sure the children are freed before their parents in the all application classes could require big efforts?

                                    J K 2 Replies Last reply 14 Apr 2020, 17:24
                                    0
                                    • J Jimmy Loyola
                                      14 Apr 2020, 15:17

                                      @kshegunov said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                                      make sure the child is freed (by the stack) before the parent

                                      What do you thinkg about make sure the children are freed before their parents in the all application classes could require big efforts?

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 14 Apr 2020, 17:24 last edited by
                                      #17

                                      @Jimmy-Loyola
                                      @kshegunov is talking about "freed by the stack". That just means scope exit for local variables. So long as you add stack children after parent, not set parent on a child, this is the natural way function calls will arrange, is it not?

                                      J 1 Reply Last reply 14 Apr 2020, 17:51
                                      0
                                      • J Jimmy Loyola
                                        14 Apr 2020, 15:17

                                        @kshegunov said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                                        make sure the child is freed (by the stack) before the parent

                                        What do you thinkg about make sure the children are freed before their parents in the all application classes could require big efforts?

                                        K Offline
                                        K Offline
                                        kshegunov
                                        Moderators
                                        wrote on 14 Apr 2020, 17:37 last edited by
                                        #18

                                        @Jimmy-Loyola said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                                        What do you thinkg about make sure the children are freed before their parents in the all application classes could require big efforts?

                                        Not really. If you stick to the "child is a member of parent object" (as I'd shown) you wouldn't have much trouble. Still caution is advised if objects are given to Qt for say to be inserted in a tab widget, because usually these containers take ownership of the object you pass them.

                                        @JonB said in Pro and cons about creating QObjects components on the stack instead of creating them dynamically.:

                                        @kshegunov is talking about "freed by the stack". That just means scope exit for local variables. So long as you add stack children after parent, not set parent on a child, this is the natural way function calls will arrange, is it not?

                                        Quite correct, yes. So like this:

                                        QObject parent(nullptr); // Root object, no parent
                                        QObject child1(&parent);
                                        QObject child2(&child1);
                                        

                                        By the natural way the stack is constructed you destroy them backwards, so you always destroy the children before the parent and you're perfectly fine. Compare with:

                                        QObject child1(nullptr); // Why no parent if it's a child
                                        QObject parent(nullptr); // First to go out of scope, the bells should be ringing in your ears already
                                        child.setParent(&parent); // Oh, no, no, I didn't mean to break the stack when we exit ...
                                        

                                        Read and abide by the Qt Code of Conduct

                                        1 Reply Last reply
                                        4
                                        • J JonB
                                          14 Apr 2020, 17:24

                                          @Jimmy-Loyola
                                          @kshegunov is talking about "freed by the stack". That just means scope exit for local variables. So long as you add stack children after parent, not set parent on a child, this is the natural way function calls will arrange, is it not?

                                          J Offline
                                          J Offline
                                          Jimmy Loyola
                                          wrote on 14 Apr 2020, 17:51 last edited by
                                          #19

                                          @JonB Right, the last one thing, why should I set a parent to a stacked QObject if I don't want that qt manages memory for it?
                                          I mean, if I never set a parent for stacked qobject the order of the objects creation doesn't matter because they aren't in the qt object tree. I am right or not? Thanks

                                          J K 2 Replies Last reply 14 Apr 2020, 17:58
                                          0
                                          • J Jimmy Loyola
                                            14 Apr 2020, 17:51

                                            @JonB Right, the last one thing, why should I set a parent to a stacked QObject if I don't want that qt manages memory for it?
                                            I mean, if I never set a parent for stacked qobject the order of the objects creation doesn't matter because they aren't in the qt object tree. I am right or not? Thanks

                                            J Offline
                                            J Offline
                                            JonB
                                            wrote on 14 Apr 2020, 17:58 last edited by JonB
                                            #20

                                            @Jimmy-Loyola
                                            Yes, that in itself is true, if you have stack variables with no parentage it won't matter about destruction order.

                                            But as per @kshegunov code, in order to use these objects your code may have to set up parentage (or call Qt functions which implicitly do) as it goes along, and then the order will matter.

                                            1 Reply Last reply
                                            0

                                            1/24

                                            14 Apr 2020, 09:52

                                            • Login

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