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. Removing custom widget from QList, will it be deleted from memory?
Forum Updated to NodeBB v4.3 + New Features

Removing custom widget from QList, will it be deleted from memory?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.3k Views 1 Watching
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    No, there's no reason for it to delete anything. You just remove the pointer from the list. It has nothing to do with the object being pointed at.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    J 1 Reply Last reply
    1
    • SGaistS SGaist

      Hi,

      No, there's no reason for it to delete anything. You just remove the pointer from the list. It has nothing to do with the object being pointed at.

      J Offline
      J Offline
      Jc_Opc
      wrote on last edited by
      #3

      @SGaist thanks, the obvious question would be, what about the memory where those objects are being stored? should it be free?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Do you mean the memory used to store the pointer internally in your a QList ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jc_Opc
          wrote on last edited by
          #5

          No, the memory of object when it was created. I assuming that QList can handle its memory resources properly.

          SGaistS 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            It's not the job of QList to manage the memory of an object behind a pointer, that is yours.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            JonBJ 1 Reply Last reply
            2
            • J Jc_Opc

              No, the memory of object when it was created. I assuming that QList can handle its memory resources properly.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @Jc_Opc said in Removing custom widget from QList, will it be deleted from memory?:

              No, the memory of object when it was created. I assuming that QList can handle its memory resources properly.

              We might be talking of two different things please clarify what you have in mind with a code sample.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • SGaistS SGaist

                It's not the job of QList to manage the memory of an object behind a pointer, that is yours.

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

                @Jc_Opc

                @SGaist said in Removing custom widget from QList, will it be deleted from memory?:

                It's not the job of QList to manage the memory of an object behind a pointer, that is yours.

                This is the important thing. The fact you put the object into/take it out of a QList<> simply has no effect on the situation if there were no list.

                You went:

                cWidget *p_widget = new cWidget(id, this); 
                

                so in principle it's your responsibility to delete it. In this case, if you have attached it to the parent by passing this, so alternatively it will get deleted if/when that gets deleted.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jc_Opc
                  wrote on last edited by
                  #9

                  thank you all, that was the reason why I put this, but bear with me a little more, I just want to know how to manage memory heap properly when using a containers (good practices). This is my attempt deleteWidget() function assuming that I am not using this when creating the widget so I would do:

                  After leaving the addWidget function, I only have the list of widgets, so here it is:

                  void parent::deleteWidget(int id){
                         delete list.at(id);
                         list.removeAt(id);
                  } 
                  

                  The reason of my confusion is because I always created each widget with its own pointer name, in here the name is the same but stored dynamically in a container.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @Jc_Opc said in Removing custom widget from QList, will it be deleted from memory?:

                    void parent::deleteWidget(int id){
                    delete list.at(id);
                    list.removeAt(id);
                    }

                    
                    void parent::deleteWidget(int id){
                           QWidget *widget = list.takeAt(id);
                           delete widget;
                    } 
                    
                    

                    Would be cleaner. You can even make it a one liner.

                    One unrelated thing: please use meaningful name for classes and variables. It will make your code easier to work with.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    J 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      @Jc_Opc said in Removing custom widget from QList, will it be deleted from memory?:

                      void parent::deleteWidget(int id){
                      delete list.at(id);
                      list.removeAt(id);
                      }

                      
                      void parent::deleteWidget(int id){
                             QWidget *widget = list.takeAt(id);
                             delete widget;
                      } 
                      
                      

                      Would be cleaner. You can even make it a one liner.

                      One unrelated thing: please use meaningful name for classes and variables. It will make your code easier to work with.

                      J Offline
                      J Offline
                      Jc_Opc
                      wrote on last edited by
                      #11

                      @SGaist Thanks, in my code the names are different, and are kind of harder to understand based on the context, I am still learning how to name objects properly though; looking at others codes and practice will do the trick. In here I just wanted to learn the principles and your answer and @JonB's helped me a lot.

                      The time you guys take to answer all these questions are very appreciated.

                      1 Reply Last reply
                      0

                      • Login

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