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 free
Forum Updated to NodeBB v4.3 + New Features

Memory free

Scheduled Pinned Locked Moved Solved General and Desktop
36 Posts 7 Posters 4.3k Views 3 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.
  • 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 Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #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 Reply Last reply
    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 Offline
      J Offline
      Joe von Habsburg
      wrote on last edited by 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 Reply Last reply
      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 Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #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 Reply Last reply
        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 Offline
          J Offline
          Joe von Habsburg
          wrote on last edited by 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 Reply Last reply
          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 Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #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 Reply Last reply
            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 Offline
              J Offline
              Joe von Habsburg
              wrote on last edited by
              #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 Reply Last reply
              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 Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #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 Reply Last reply
                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 Offline
                  J Offline
                  Joe von Habsburg
                  wrote on last edited by 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 Replies Last reply
                  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 Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #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 Reply Last reply
                    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 Offline
                      J Offline
                      Joe von Habsburg
                      wrote on last edited by 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 Reply Last reply
                      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 Offline
                        S Offline
                        SimonSchroeder
                        wrote on last edited by
                        #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 Reply Last reply
                        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 Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #36

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

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

                          1 Reply Last reply
                          1

                          • Login

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