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. QHash and memory
QtWS25 Last Chance

QHash and memory

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 642 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.
  • T Offline
    T Offline
    taraj
    wrote on last edited by
    #1

    Hello,

    I have the following defined in my header file.

    QHash<int , Data*> *DataHash;
    

    In my cpp constructor I have this:

    DataHash = new(QHash <int, Data*>);
    

    When i receive data I insert into the qhash by putting it in a new data and insert it into the qhash

    data= new Data(a, b, c); 
    DataHash->insert(number, data); 
    

    Every x seconds I need to search the qhash for old data and remove it.

          QHash<int, Data *>::iterator it = DataHash->begin();
          while (it != FlightDataHash->end())
          {
             if(nowTime > t)
                {
                Data *tmp = DataHash->remove(it.value()->getTrackNumber()); //remove, erase or take?
                delete tmp;
            // it = FlightDataHash->erase(it); //Seg fault: QUESTION do i need something like this or is "delete tmp" enough to remove memory?
           }
    

    My question is will the remove and the delete tmp be enough to ensure I don't have a memory leak from when i created the "data= new Data(a, b, c); "
    Is using remove() the correct call or is take() or erase() better?

    Thank you very much

    jsulmJ 1 Reply Last reply
    0
    • T taraj

      Hello,

      I have the following defined in my header file.

      QHash<int , Data*> *DataHash;
      

      In my cpp constructor I have this:

      DataHash = new(QHash <int, Data*>);
      

      When i receive data I insert into the qhash by putting it in a new data and insert it into the qhash

      data= new Data(a, b, c); 
      DataHash->insert(number, data); 
      

      Every x seconds I need to search the qhash for old data and remove it.

            QHash<int, Data *>::iterator it = DataHash->begin();
            while (it != FlightDataHash->end())
            {
               if(nowTime > t)
                  {
                  Data *tmp = DataHash->remove(it.value()->getTrackNumber()); //remove, erase or take?
                  delete tmp;
              // it = FlightDataHash->erase(it); //Seg fault: QUESTION do i need something like this or is "delete tmp" enough to remove memory?
             }
      

      My question is will the remove and the delete tmp be enough to ensure I don't have a memory leak from when i created the "data= new Data(a, b, c); "
      Is using remove() the correct call or is take() or erase() better?

      Thank you very much

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @taraj

      1. Remove the data from the QHash:DataHasht.erase(it) (see example in https://doc.qt.io/qt-5/qhash-iterator.html)
      2. Delete data itself: delete tmp;

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

      T 1 Reply Last reply
      1
      • jsulmJ jsulm

        @taraj

        1. Remove the data from the QHash:DataHasht.erase(it) (see example in https://doc.qt.io/qt-5/qhash-iterator.html)
        2. Delete data itself: delete tmp;
        T Offline
        T Offline
        taraj
        wrote on last edited by
        #3

        @jsulm
        So i do:

        it = FlightDataHash->erase(it);
        

        which removes the item pointed to by the iterator from the hash

        but how to I get Data tmp from the iterator to do number 2 in your response? As i can't do

        delete it;
        

        thank you.

        JonBJ jsulmJ 2 Replies Last reply
        0
        • T taraj

          @jsulm
          So i do:

          it = FlightDataHash->erase(it);
          

          which removes the item pointed to by the iterator from the hash

          but how to I get Data tmp from the iterator to do number 2 in your response? As i can't do

          delete it;
          

          thank you.

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

          @taraj
          delete the data pointer before doing the iterator erase().

          1 Reply Last reply
          1
          • T taraj

            @jsulm
            So i do:

            it = FlightDataHash->erase(it);
            

            which removes the item pointed to by the iterator from the hash

            but how to I get Data tmp from the iterator to do number 2 in your response? As i can't do

            delete it;
            

            thank you.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @taraj Please read carefully https://doc.qt.io/qt-5/qhash.html#erase , then you will know what you're doing wrong now!

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

            T 1 Reply Last reply
            0
            • jsulmJ jsulm

              @taraj Please read carefully https://doc.qt.io/qt-5/qhash.html#erase , then you will know what you're doing wrong now!

              T Offline
              T Offline
              taraj
              wrote on last edited by
              #6

              @jsulm
              "Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator to the next item in the hash."
              where does it remove the data to? How can I access the data to delete it from memory?

              FlightData *tmp = FlightDataHash->remove(it.value()->getTrackNumber());
              delete tmp;
              it = FlightDataHash->erase(it); //This will seg fault
              

              i'm back to the above, this is what i had in the original post?

              jsulmJ 1 Reply Last reply
              0
              • T taraj

                @jsulm
                "Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator to the next item in the hash."
                where does it remove the data to? How can I access the data to delete it from memory?

                FlightData *tmp = FlightDataHash->remove(it.value()->getTrackNumber());
                delete tmp;
                it = FlightDataHash->erase(it); //This will seg fault
                

                i'm back to the above, this is what i had in the original post?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @taraj said in QHash and memory:

                FlightData *tmp = FlightDataHash->remove(it.value()->getTrackNumber());

                Shouldn't it be

                FlightData *tmp = it.value();
                delete tmp;
                it = FlightDataHash->erase(it);
                

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

                JonBJ 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @taraj said in QHash and memory:

                  FlightData *tmp = FlightDataHash->remove(it.value()->getTrackNumber());

                  Shouldn't it be

                  FlightData *tmp = it.value();
                  delete tmp;
                  it = FlightDataHash->erase(it);
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @jsulm said in QHash and memory:

                  @taraj said in QHash and memory:

                  FlightData *tmp = FlightDataHash->remove(it.value()->getTrackNumber());

                  Shouldn't it be

                  FlightData *tmp = it.value();
                  delete tmp;
                  it = FlightDataHash->erase(it);
                  

                  Just as I said:

                  @taraj

                  delete the data pointer before doing the iterator erase().

                  1 Reply Last reply
                  2

                  • Login

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