跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  2. Qt Development
  3. General and Desktop
  4. Memory free
Forum Updated to NodeBB v4.3 + New Features

Memory free

已排程 已置頂 已鎖定 已移動 Solved General and Desktop
36 貼文 7 Posters 4.0k 瀏覽 3 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • S SimonSchroeder

    @Pl45m4 said in Memory free:

    My point was, when elements in the container are QObjects with a parent, there is nothing to do and you don't have to delete them manually.

    This is only partially true. In the OPs case he continuously adds and removes QObjects to a container. I would expect that the parent will live a lot longer. Thus, deleting the objects manually once they are removed from the container is a good idea. If the parent lives until the end of the program, you would never free any memory automatically. (The OP posted code for deleteLast() that both removes the object from the container and calls deleteLater() on it.)

    @Joe-von-Habsburg said in Memory free:

    So many times I clicked the buttons. add delete add delete add delete .... maybe a hundred times

    If you are are alternating between clicking add and delete, the amount of memory used should not change (you seem to be running the event loop as clicks are registered; and thus deleteLater() should do its job). Only clicking add repeatedly should increase memory. If you want help with your code we would need a minimal compilable example that reproduces the problem. There could be something wrong anywhere in you whole code.

    On Windows the best tool (at least the best tool I know) for debugging is Visual Studio. Debugging inside Qt Creator is awfully slow on Windows (the slower the larger your projects gets). With qmake-based projects you can call qmake on the command line to create a fresh VS project and run that to debug or inspect memory usage (or profile performance). With CMake-based projects you can just open them up inside VS.

    Pl45m4P 離線
    Pl45m4P 離線
    Pl45m4
    寫於 最後由 編輯
    #20

    @SimonSchroeder said in Memory free:

    This is only partially true. In the OPs case he continuously adds and removes QObjects to a container. I would expect that the parent will live a lot longer. Thus, deleting the objects manually once they are removed from the container is a good idea. If the parent lives until the end of the program, you would never free any memory automatically. (The OP posted code for deleteLast() that both removes the object from the container and calls deleteLater() on it.)

    Yes, and I dont want to start nitpicking... but I didn't wrote anything else :)
    Of course, when you want to re-create the instance, the old one should be free'd correctly. At the very beginning @Joe-von-Habsburg also posted a snippet where he showed deleteLater() on a stack member inside class destructor :)


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    1 條回覆 最後回覆
    1
    • JonBJ JonB

      @Joe-von-Habsburg said in Memory free:

      whats your recomended without valgrid ?

      I answered that previously if you are using MSVC/VS under Windows. If you are using MinGW I don't know. I do not use Qt under Windows.

      Is memory leak able to be for that reason ? Must I delete or do something ?

      The code you show (seems to) just access an element in whatever type of collection myClasses is (apparently, a collection of MyClass * pointers). There is no "leak" here in itself.

      J 離線
      J 離線
      Joe von Habsburg
      寫於 最後由 編輯
      #21

      Thanks everyone for try help me :)

      @JonB said in Memory free:

      I answered that previously if you are using MSVC/VS under Windows.

      I use MSVC compiler with QtCreator. I do not use Visual Studio.

      @JonB said in Memory free:

      The code you show (seems to) just access an element in whatever type of collection myClasses is (apparently, a collection of MyClass * pointers). There is no "leak" here in itself.

      Thank you for answered my possible thought.

      @Pl45m4 said in Memory free:

      Depends on how you create the objects in myClasses.

      I create objects with click add button as pointer.

      @Pl45m4 said in Memory free:

      myClasses is a Q-container of QObjects?!

      myClasses is QList.

      QList<MyClass *> myClasses;
      

      @Pl45m4 said in Memory free:

      If they are QObjects and have a parent, then you don't have to do anything, because they will be deleted when parent is deleted.

      MyClass is QObject.

      class MyClass : public QObject{}
      

      When I use delete button, I delete the last index of myClasses list. It must be deleted.

      void deleteLast(){
        MyClass *mc = myClasses.last();
        deleteConnections(mc);
        mc->deleteLater();
        myClasses.removeLast();
      }
      

      @Pl45m4 said in Memory free:

      But be careful not to access the pointers in other places, if so. Always check for nullptr or use something like QSharedPointer when passing QObjects around.

      Its assignment only deconstructer as nullptr. Should not I ?

      ~MyClass(){
         //delete all probs like that:
         someThingPointer->deleteLater();
         someThingPointer = nullptr;
         someThing.deleteLater();
       }
      

      @Pl45m4 said in Memory free:

      I would recommend you to read this:

      Thank you I will read.

      @JonB said in Memory free:

      The OP asked about memory leak or deleting because that what this topic is about, I understand what you are saying [perfectly valid advice] but just don't see how they are related.

      I was just curious. by assigning an object with a pointer according to the index of the list in a loop. will using a method of that object and then not doing anything to that object after I'm done and not leaving it there cause a leak? But it won't happen.

      @Pl45m4 said in Memory free:

      My point was, when elements in the container are QObjects with a parent, there is nothing to do and you don't have to delete them manually.

      You want to say, If I delete MyClass, all properties automaticly deleted. I don't have to do anything.

      @SimonSchroeder said in Memory free:

      (The OP posted code for deleteLast() that both removes the object from the container and calls deleteLater() on it.)

      Yes I do it, But memory cant be free. Always increase. (deconstructer is working)

      void deleteLast(){
        MyClass *mc = myClasses.last();
        deleteConnections(mc);
        mc->deleteLater();
        myClasses.removeLast();
      }
      

      @SimonSchroeder said in Memory free:

      If you are are alternating between clicking add and delete, the amount of memory used should not change

      I tested like that : add add add remove remove remove add add add remove remove remove... (myClasses size can be min 1, max 4). But always increase. Memory cannot be free.

      @SimonSchroeder said in Memory free:

      If you want help with your code we would need a minimal compilable example that reproduces the problem. There could be something wrong anywhere in you whole code.

      Okey, I will make basic code example and I will try.

      @SimonSchroeder said in Memory free:

      Debugging inside Qt Creator is awfully slow on Windows

      Is driving me crazy, and I can't use the debug profile right now for some reasons..

      @SimonSchroeder said in Memory free:

      With CMake-based projects you can just open them up inside VS.

      I work with CMake. If I open the project in VS, I don't have to anything. It will work ?

      @Helmut-Jakoby said in Memory free:

      I once took the trouble to write a "Memory Leak Indicator" for "new/delete" that shows the file name and line number in which an object that has not been released was instantiated.

      Thank you I will look.

      JonBJ Pl45m4P 2 條回覆 最後回覆
      0
      • J Joe von Habsburg

        Thanks everyone for try help me :)

        @JonB said in Memory free:

        I answered that previously if you are using MSVC/VS under Windows.

        I use MSVC compiler with QtCreator. I do not use Visual Studio.

        @JonB said in Memory free:

        The code you show (seems to) just access an element in whatever type of collection myClasses is (apparently, a collection of MyClass * pointers). There is no "leak" here in itself.

        Thank you for answered my possible thought.

        @Pl45m4 said in Memory free:

        Depends on how you create the objects in myClasses.

        I create objects with click add button as pointer.

        @Pl45m4 said in Memory free:

        myClasses is a Q-container of QObjects?!

        myClasses is QList.

        QList<MyClass *> myClasses;
        

        @Pl45m4 said in Memory free:

        If they are QObjects and have a parent, then you don't have to do anything, because they will be deleted when parent is deleted.

        MyClass is QObject.

        class MyClass : public QObject{}
        

        When I use delete button, I delete the last index of myClasses list. It must be deleted.

        void deleteLast(){
          MyClass *mc = myClasses.last();
          deleteConnections(mc);
          mc->deleteLater();
          myClasses.removeLast();
        }
        

        @Pl45m4 said in Memory free:

        But be careful not to access the pointers in other places, if so. Always check for nullptr or use something like QSharedPointer when passing QObjects around.

        Its assignment only deconstructer as nullptr. Should not I ?

        ~MyClass(){
           //delete all probs like that:
           someThingPointer->deleteLater();
           someThingPointer = nullptr;
           someThing.deleteLater();
         }
        

        @Pl45m4 said in Memory free:

        I would recommend you to read this:

        Thank you I will read.

        @JonB said in Memory free:

        The OP asked about memory leak or deleting because that what this topic is about, I understand what you are saying [perfectly valid advice] but just don't see how they are related.

        I was just curious. by assigning an object with a pointer according to the index of the list in a loop. will using a method of that object and then not doing anything to that object after I'm done and not leaving it there cause a leak? But it won't happen.

        @Pl45m4 said in Memory free:

        My point was, when elements in the container are QObjects with a parent, there is nothing to do and you don't have to delete them manually.

        You want to say, If I delete MyClass, all properties automaticly deleted. I don't have to do anything.

        @SimonSchroeder said in Memory free:

        (The OP posted code for deleteLast() that both removes the object from the container and calls deleteLater() on it.)

        Yes I do it, But memory cant be free. Always increase. (deconstructer is working)

        void deleteLast(){
          MyClass *mc = myClasses.last();
          deleteConnections(mc);
          mc->deleteLater();
          myClasses.removeLast();
        }
        

        @SimonSchroeder said in Memory free:

        If you are are alternating between clicking add and delete, the amount of memory used should not change

        I tested like that : add add add remove remove remove add add add remove remove remove... (myClasses size can be min 1, max 4). But always increase. Memory cannot be free.

        @SimonSchroeder said in Memory free:

        If you want help with your code we would need a minimal compilable example that reproduces the problem. There could be something wrong anywhere in you whole code.

        Okey, I will make basic code example and I will try.

        @SimonSchroeder said in Memory free:

        Debugging inside Qt Creator is awfully slow on Windows

        Is driving me crazy, and I can't use the debug profile right now for some reasons..

        @SimonSchroeder said in Memory free:

        With CMake-based projects you can just open them up inside VS.

        I work with CMake. If I open the project in VS, I don't have to anything. It will work ?

        @Helmut-Jakoby said in Memory free:

        I once took the trouble to write a "Memory Leak Indicator" for "new/delete" that shows the file name and line number in which an object that has not been released was instantiated.

        Thank you I will look.

        JonBJ 離線
        JonBJ 離線
        JonB
        寫於 最後由 編輯
        #22

        @Joe-von-Habsburg said in Memory free:

        I use MSVC compiler with QtCreator. I do not use Visual Studio.

        Then as everybody has said you are going to find it very difficult to find a suitable tool/environment to use for memory usage checking under Windows.

        1 條回覆 最後回覆
        0
        • J Joe von Habsburg

          Thanks everyone for try help me :)

          @JonB said in Memory free:

          I answered that previously if you are using MSVC/VS under Windows.

          I use MSVC compiler with QtCreator. I do not use Visual Studio.

          @JonB said in Memory free:

          The code you show (seems to) just access an element in whatever type of collection myClasses is (apparently, a collection of MyClass * pointers). There is no "leak" here in itself.

          Thank you for answered my possible thought.

          @Pl45m4 said in Memory free:

          Depends on how you create the objects in myClasses.

          I create objects with click add button as pointer.

          @Pl45m4 said in Memory free:

          myClasses is a Q-container of QObjects?!

          myClasses is QList.

          QList<MyClass *> myClasses;
          

          @Pl45m4 said in Memory free:

          If they are QObjects and have a parent, then you don't have to do anything, because they will be deleted when parent is deleted.

          MyClass is QObject.

          class MyClass : public QObject{}
          

          When I use delete button, I delete the last index of myClasses list. It must be deleted.

          void deleteLast(){
            MyClass *mc = myClasses.last();
            deleteConnections(mc);
            mc->deleteLater();
            myClasses.removeLast();
          }
          

          @Pl45m4 said in Memory free:

          But be careful not to access the pointers in other places, if so. Always check for nullptr or use something like QSharedPointer when passing QObjects around.

          Its assignment only deconstructer as nullptr. Should not I ?

          ~MyClass(){
             //delete all probs like that:
             someThingPointer->deleteLater();
             someThingPointer = nullptr;
             someThing.deleteLater();
           }
          

          @Pl45m4 said in Memory free:

          I would recommend you to read this:

          Thank you I will read.

          @JonB said in Memory free:

          The OP asked about memory leak or deleting because that what this topic is about, I understand what you are saying [perfectly valid advice] but just don't see how they are related.

          I was just curious. by assigning an object with a pointer according to the index of the list in a loop. will using a method of that object and then not doing anything to that object after I'm done and not leaving it there cause a leak? But it won't happen.

          @Pl45m4 said in Memory free:

          My point was, when elements in the container are QObjects with a parent, there is nothing to do and you don't have to delete them manually.

          You want to say, If I delete MyClass, all properties automaticly deleted. I don't have to do anything.

          @SimonSchroeder said in Memory free:

          (The OP posted code for deleteLast() that both removes the object from the container and calls deleteLater() on it.)

          Yes I do it, But memory cant be free. Always increase. (deconstructer is working)

          void deleteLast(){
            MyClass *mc = myClasses.last();
            deleteConnections(mc);
            mc->deleteLater();
            myClasses.removeLast();
          }
          

          @SimonSchroeder said in Memory free:

          If you are are alternating between clicking add and delete, the amount of memory used should not change

          I tested like that : add add add remove remove remove add add add remove remove remove... (myClasses size can be min 1, max 4). But always increase. Memory cannot be free.

          @SimonSchroeder said in Memory free:

          If you want help with your code we would need a minimal compilable example that reproduces the problem. There could be something wrong anywhere in you whole code.

          Okey, I will make basic code example and I will try.

          @SimonSchroeder said in Memory free:

          Debugging inside Qt Creator is awfully slow on Windows

          Is driving me crazy, and I can't use the debug profile right now for some reasons..

          @SimonSchroeder said in Memory free:

          With CMake-based projects you can just open them up inside VS.

          I work with CMake. If I open the project in VS, I don't have to anything. It will work ?

          @Helmut-Jakoby said in Memory free:

          I once took the trouble to write a "Memory Leak Indicator" for "new/delete" that shows the file name and line number in which an object that has not been released was instantiated.

          Thank you I will look.

          Pl45m4P 離線
          Pl45m4P 離線
          Pl45m4
          寫於 最後由 編輯
          #23

          @Joe-von-Habsburg said in Memory free:

          @Pl45m4 said in Memory free:

          Depends on how you create the objects in myClasses.

          I create objects with click add button as pointer.

          Codewise... not where you click to add it to your list.
          Somewhere in your code you create the object.

          // "this" as parent, when parent is destroyed, "myC" also cleaned up.
          MyClass *myC = new MyClass(this); 
          // or
          MyClass *myC = new MyClass(); // no parent
          

          As @SimonSchroeder has written above, it's diffent when deleting and re-creating myC multiple times, while parent is "alive" the whole time. Then you have to delete it yourself.
          But, refering to your initial code snippet in your first post, in a destructor you dont need to do it


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          J 1 條回覆 最後回覆
          0
          • Pl45m4P Pl45m4

            @Joe-von-Habsburg said in Memory free:

            @Pl45m4 said in Memory free:

            Depends on how you create the objects in myClasses.

            I create objects with click add button as pointer.

            Codewise... not where you click to add it to your list.
            Somewhere in your code you create the object.

            // "this" as parent, when parent is destroyed, "myC" also cleaned up.
            MyClass *myC = new MyClass(this); 
            // or
            MyClass *myC = new MyClass(); // no parent
            

            As @SimonSchroeder has written above, it's diffent when deleting and re-creating myC multiple times, while parent is "alive" the whole time. Then you have to delete it yourself.
            But, refering to your initial code snippet in your first post, in a destructor you dont need to do it

            J 離線
            J 離線
            Joe von Habsburg
            寫於 最後由 Joe von Habsburg 編輯
            #24

            @Pl45m4 @JonB @Helmut-Jakoby @SimonSchroeder @jsulm

            I solved the problem. I used deleteLater(), clear, nullptr but they did not work. I use just delete and than it works.

            ~MyClass(){
             delete sth;
            }
            

            Thanks for your helps.

            JonBJ 1 條回覆 最後回覆
            0
            • J Joe von Habsburg has marked this topic as solved on
            • J Joe von Habsburg

              @Pl45m4 @JonB @Helmut-Jakoby @SimonSchroeder @jsulm

              I solved the problem. I used deleteLater(), clear, nullptr but they did not work. I use just delete and than it works.

              ~MyClass(){
               delete sth;
              }
              

              Thanks for your helps.

              JonBJ 離線
              JonBJ 離線
              JonB
              寫於 最後由 編輯
              #25

              @Joe-von-Habsburg
              OK, but you never said what type your sth is. If it's not a QObject * you cannot call deleteLater() on it anyway. If it is, and deleteLater() fails to delete it, then (presumably) you do not allow return to the top-level Qt event loop after you have called it: don't know where/when your destructor is called.

              J 1 條回覆 最後回覆
              0
              • JonBJ JonB

                @Joe-von-Habsburg
                OK, but you never said what type your sth is. If it's not a QObject * you cannot call deleteLater() on it anyway. If it is, and deleteLater() fails to delete it, then (presumably) you do not allow return to the top-level Qt event loop after you have called it: don't know where/when your destructor is called.

                J 離線
                J 離線
                Joe von Habsburg
                寫於 最後由 Joe von Habsburg 編輯
                #26

                @JonB said in Memory free:

                sth

                some sth was QObject, some sth was kind of list (QVector<double> *). My opinion, I cleared list when I delete MyClass, but they took the memory and did not release.

                // did not work
                ~MyClass(){
                  sthPointer->deleteLater();
                  sthPointer = nullptr;
                
                  sthList->clear();
                  sthList = nullptr;
                }
                
                //Work
                ~MyClass(){
                  sthPointer->deleteLater();
                  sthPointer = nullptr;
                
                  delete sthList;
                }
                
                Christian EhrlicherC 1 條回覆 最後回覆
                0
                • J Joe von Habsburg

                  @JonB said in Memory free:

                  sth

                  some sth was QObject, some sth was kind of list (QVector<double> *). My opinion, I cleared list when I delete MyClass, but they took the memory and did not release.

                  // did not work
                  ~MyClass(){
                    sthPointer->deleteLater();
                    sthPointer = nullptr;
                  
                    sthList->clear();
                    sthList = nullptr;
                  }
                  
                  //Work
                  ~MyClass(){
                    sthPointer->deleteLater();
                    sthPointer = nullptr;
                  
                    delete sthList;
                  }
                  
                  Christian EhrlicherC 離線
                  Christian EhrlicherC 離線
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  寫於 最後由 編輯
                  #27

                  @Joe-von-Habsburg said in Memory free:

                  I cleared list when I delete MyClass, but they took the memory and did not release.

                  Which is basic c++. Why do you create the container on the heap at all? That's unusual, not needed and error prone as you can see.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  J 1 條回覆 最後回覆
                  3
                  • Christian EhrlicherC Christian Ehrlicher

                    @Joe-von-Habsburg said in Memory free:

                    I cleared list when I delete MyClass, but they took the memory and did not release.

                    Which is basic c++. Why do you create the container on the heap at all? That's unusual, not needed and error prone as you can see.

                    J 離線
                    J 離線
                    Joe von Habsburg
                    寫於 最後由 Joe von Habsburg 編輯
                    #28

                    @Christian-Ehrlicher said in Memory free:

                    Which is basic c++. Why do you create the container on the heap at all? That's unusual, not needed and error prone as you can see.

                    I have 2 thing. Firstly a class => MyClass
                    Secondly => ClassController (another QObject)

                    My system like that:

                    //MyClass
                    class MyClass : QObject{
                     MyClass(){
                    someThingPointer = new SomeThingPointer ;
                       someThingList = new SomeThingList<double>;
                     };
                    ~MyClass(){
                    someThingPointer->deleteLater();
                        delete someThingList ;
                     };
                    
                    void doSth();
                    
                    private :
                     SomeThingPointer *someThingPointer;
                     SomeThingList<double> *someThingList;
                    }
                    
                    //ClassController
                    class ClassController: QObject{
                     ClassController();
                    ~ClassController();
                    
                    void run();
                    
                    public slots:
                    void addNew();
                    void deleteLast();
                    
                    private :
                    QList<MyClass *> myClasses;
                    }
                    
                    
                    //MainWindow
                    void on_add_click(){
                     emit addNew();
                    }
                    
                    void on_delete_click(){
                     emit deleteLast();
                    }
                    

                    Where should I create new list? I create in constructer

                    @Christian-Ehrlicher said in Memory free:

                    Why do you create the container on the heap at all?

                    What do you mean by that? Can you explain?

                    jsulmJ 1 條回覆 最後回覆
                    0
                    • J Joe von Habsburg

                      @Christian-Ehrlicher said in Memory free:

                      Which is basic c++. Why do you create the container on the heap at all? That's unusual, not needed and error prone as you can see.

                      I have 2 thing. Firstly a class => MyClass
                      Secondly => ClassController (another QObject)

                      My system like that:

                      //MyClass
                      class MyClass : QObject{
                       MyClass(){
                      someThingPointer = new SomeThingPointer ;
                         someThingList = new SomeThingList<double>;
                       };
                      ~MyClass(){
                      someThingPointer->deleteLater();
                          delete someThingList ;
                       };
                      
                      void doSth();
                      
                      private :
                       SomeThingPointer *someThingPointer;
                       SomeThingList<double> *someThingList;
                      }
                      
                      //ClassController
                      class ClassController: QObject{
                       ClassController();
                      ~ClassController();
                      
                      void run();
                      
                      public slots:
                      void addNew();
                      void deleteLast();
                      
                      private :
                      QList<MyClass *> myClasses;
                      }
                      
                      
                      //MainWindow
                      void on_add_click(){
                       emit addNew();
                      }
                      
                      void on_delete_click(){
                       emit deleteLast();
                      }
                      

                      Where should I create new list? I create in constructer

                      @Christian-Ehrlicher said in Memory free:

                      Why do you create the container on the heap at all?

                      What do you mean by that? Can you explain?

                      jsulmJ 離線
                      jsulmJ 離線
                      jsulm
                      Lifetime Qt Champion
                      寫於 最後由 編輯
                      #29

                      @Joe-von-Habsburg said in Memory free:

                      What do you mean by that? Can you explain?

                      Basic C++ stuff:

                      // This creates a member variable which is a pointer to a list
                      // If you then do someThingList = new ... you allocate memory on the heap
                      SomeThingList<double> *someThingList;
                      

                      And the question was: why do you allocate someThingList on the heap? So, why is someThingList a pointer and not simply:

                      SomeThingList<double> someThingList;
                      

                      Don't use pointers if they are not really needed...

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

                      J 1 條回覆 最後回覆
                      1
                      • jsulmJ jsulm

                        @Joe-von-Habsburg said in Memory free:

                        What do you mean by that? Can you explain?

                        Basic C++ stuff:

                        // This creates a member variable which is a pointer to a list
                        // If you then do someThingList = new ... you allocate memory on the heap
                        SomeThingList<double> *someThingList;
                        

                        And the question was: why do you allocate someThingList on the heap? So, why is someThingList a pointer and not simply:

                        SomeThingList<double> someThingList;
                        

                        Don't use pointers if they are not really needed...

                        J 離線
                        J 離線
                        Joe von Habsburg
                        寫於 最後由 編輯
                        #30

                        @jsulm said in Memory free:

                        Don't use pointers if they are not really needed...

                        Because I couldn't change the indexes. When I first created this class for the first time, it was simple (no pointers). But then I realized that I can't change the indexes.

                        void run(){
                          for(int i = 0; i < myClasses.size(); i++){
                            MyClass *mc = myClasses.at(i);
                            mc->someThingList()[25] = 10000;
                          }
                        }
                        

                        Do you have an advice?

                        jsulmJ 1 條回覆 最後回覆
                        0
                        • J Joe von Habsburg

                          @jsulm said in Memory free:

                          Don't use pointers if they are not really needed...

                          Because I couldn't change the indexes. When I first created this class for the first time, it was simple (no pointers). But then I realized that I can't change the indexes.

                          void run(){
                            for(int i = 0; i < myClasses.size(); i++){
                              MyClass *mc = myClasses.at(i);
                              mc->someThingList()[25] = 10000;
                            }
                          }
                          

                          Do you have an advice?

                          jsulmJ 離線
                          jsulmJ 離線
                          jsulm
                          Lifetime Qt Champion
                          寫於 最後由 編輯
                          #31

                          @Joe-von-Habsburg said in Memory free:

                          Because I couldn't change the indexes.

                          What does this have to do with pointers?
                          This should work just fine (don't know why you put () there):

                          mc->someThingList[25] = 10000;
                          

                          But as far as I can see you did not explain what SomeThingList is exactly.

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

                          J 1 條回覆 最後回覆
                          2
                          • jsulmJ jsulm

                            @Joe-von-Habsburg said in Memory free:

                            Because I couldn't change the indexes.

                            What does this have to do with pointers?
                            This should work just fine (don't know why you put () there):

                            mc->someThingList[25] = 10000;
                            

                            But as far as I can see you did not explain what SomeThingList is exactly.

                            J 離線
                            J 離線
                            Joe von Habsburg
                            寫於 最後由 Joe von Habsburg 編輯
                            #32

                            @jsulm said in Memory free:

                            don't know why you put () there

                            because it is private and access with getter.

                            @jsulm said in Memory free:

                            What does this have to do with pointers?

                            It could not work, but I will try again for you :)

                            Thanks for eveything

                            @jsulm said in Memory free:

                            What does this have to do with pointers?

                            I tested. for example :

                            for(int i = 0; i < myClasses.size(); i++){
                              MyClass *mc = myClasses.at(i);
                              for(int j = 0; j < size; j++){
                                mc ->myList().append(x);
                              }
                              qDebug() << mc ->myList().size(); // 0
                            }
                            

                            I can't add new data to mc ->myList() in classcontroller

                            jsulmJ S 2 條回覆 最後回覆
                            0
                            • J Joe von Habsburg

                              @jsulm said in Memory free:

                              don't know why you put () there

                              because it is private and access with getter.

                              @jsulm said in Memory free:

                              What does this have to do with pointers?

                              It could not work, but I will try again for you :)

                              Thanks for eveything

                              @jsulm said in Memory free:

                              What does this have to do with pointers?

                              I tested. for example :

                              for(int i = 0; i < myClasses.size(); i++){
                                MyClass *mc = myClasses.at(i);
                                for(int j = 0; j < size; j++){
                                  mc ->myList().append(x);
                                }
                                qDebug() << mc ->myList().size(); // 0
                              }
                              

                              I can't add new data to mc ->myList() in classcontroller

                              jsulmJ 離線
                              jsulmJ 離線
                              jsulm
                              Lifetime Qt Champion
                              寫於 最後由 編輯
                              #33

                              @Joe-von-Habsburg said in Memory free:

                              qDebug() << mc ->myList().size(); // 0

                              If you mean size() returns 0 then this simply means that size variable has 0 as value, so the inner for loop is never executed. Another explanation would be that myList() returns a COPY of the container, so you add to the copy (it should either return pointer to the container or reference). Again, has nothing to do with pointers...

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

                              J 1 條回覆 最後回覆
                              1
                              • jsulmJ jsulm

                                @Joe-von-Habsburg said in Memory free:

                                qDebug() << mc ->myList().size(); // 0

                                If you mean size() returns 0 then this simply means that size variable has 0 as value, so the inner for loop is never executed. Another explanation would be that myList() returns a COPY of the container, so you add to the copy (it should either return pointer to the container or reference). Again, has nothing to do with pointers...

                                J 離線
                                J 離線
                                Joe von Habsburg
                                寫於 最後由 Joe von Habsburg 編輯
                                #34

                                @jsulm said in Memory free:

                                so the inner for loop is never executed. Another explanation would be that myList() returns a COPY of the container, so you add to the copy (it should either return pointer to the container or reference). Again, has nothing to do with pointers...

                                how can I append new data to my list ? when I use pointer list, its work, but if do not use pointer, I can not append new data

                                @SimonSchroeder said in Memory free:

                                it will create a copy that is returned and you'll only change the copy. With a reference you'll change the underlying list:

                                thank you

                                jsulmJ 1 條回覆 最後回覆
                                0
                                • J Joe von Habsburg

                                  @jsulm said in Memory free:

                                  don't know why you put () there

                                  because it is private and access with getter.

                                  @jsulm said in Memory free:

                                  What does this have to do with pointers?

                                  It could not work, but I will try again for you :)

                                  Thanks for eveything

                                  @jsulm said in Memory free:

                                  What does this have to do with pointers?

                                  I tested. for example :

                                  for(int i = 0; i < myClasses.size(); i++){
                                    MyClass *mc = myClasses.at(i);
                                    for(int j = 0; j < size; j++){
                                      mc ->myList().append(x);
                                    }
                                    qDebug() << mc ->myList().size(); // 0
                                  }
                                  

                                  I can't add new data to mc ->myList() in classcontroller

                                  S 離線
                                  S 離線
                                  SimonSchroeder
                                  寫於 最後由 編輯
                                  #35

                                  @Joe-von-Habsburg said in Memory free:

                                  I can't add new data to mc ->myList() in classcontroller

                                  You need to make sure that MyClass::myList() returns a reference to the list. If you have something like:

                                  class MyClass
                                  {
                                    //...
                                    SomeThingList<double> myList() { return someThingList; }
                                  }
                                  

                                  it will create a copy that is returned and you'll only change the copy. With a reference you'll change the underlying list:

                                  class MyClass
                                  {
                                    //...
                                    SomeThingList<double>& myList() { return someThingList; }
                                  }
                                  
                                  1 條回覆 最後回覆
                                  3
                                  • J Joe von Habsburg

                                    @jsulm said in Memory free:

                                    so the inner for loop is never executed. Another explanation would be that myList() returns a COPY of the container, so you add to the copy (it should either return pointer to the container or reference). Again, has nothing to do with pointers...

                                    how can I append new data to my list ? when I use pointer list, its work, but if do not use pointer, I can not append new data

                                    @SimonSchroeder said in Memory free:

                                    it will create a copy that is returned and you'll only change the copy. With a reference you'll change the underlying list:

                                    thank you

                                    jsulmJ 離線
                                    jsulmJ 離線
                                    jsulm
                                    Lifetime Qt Champion
                                    寫於 最後由 編輯
                                    #36

                                    @Joe-von-Habsburg You should really learn C++.
                                    See what @SimonSchroeder wrote...

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

                                    1 條回覆 最後回覆
                                    1

                                    • 登入

                                    • Login or register to search.
                                    • 第一個貼文
                                      最後的貼文
                                    0
                                    • 版面
                                    • 最新
                                    • 標籤
                                    • 熱門
                                    • 使用者
                                    • 群組
                                    • 搜尋
                                    • Get Qt Extensions
                                    • Unsolved