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 pointer to members question

QHash and pointer to members question

Scheduled Pinned Locked Moved General and Desktop
qhashmemory managmen
9 Posts 4 Posters 4.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.
  • X Offline
    X Offline
    Xumepoc
    wrote on 25 Jun 2015, 17:59 last edited by
    #1

    Hi everyone,
    I have a question for you, which for some of you might be very simple, but I need to be sure.

    I have a QHash list with some members inside. Now I need to pass a pointers pointing to some of the members in the list, my question is what will happen if one or more of the members in the list are removed? Will the removal of one member re-shifts the members after it in the memory, resulting in pointer pointing to a wrong memory?

    Todor

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Jun 2015, 20:46 last edited by
      #2

      Hi,

      Can you share a code sample that shows what you are trying to do ?

      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
      • X Offline
        X Offline
        Xumepoc
        wrote on 27 Jun 2015, 10:08 last edited by
        #3

        I will, but I am away from my PC this week :(

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 27 Jun 2015, 13:01 last edited by
          #4

          Modifying contents of a QHash invalidates (potentially) any pointers pointing into it as the memory needed to store the hash table could be reallocated for growth (or shrinking).

          You can read more about it here. Every time iterator invalidation is mentioned you can assume it also applies to pointers into the container. It is generally not safe to keep pointers to elements in containers.

          Few alternatives are storing pointers in the containers, using uuids and find instead of pointers or use of non-mutable size containers like std::array<T>, all depending on your particular use case and complexity requirements.

          1 Reply Last reply
          0
          • X Offline
            X Offline
            Xumepoc
            wrote on 1 Jul 2015, 17:31 last edited by Xumepoc 7 Jan 2015, 17:35
            #5

            I am not storing pointers in the QHash, I store a real genuine copy of the objects, but I pass pointers to different executable classes which can live 30 minutes or more, but during that time the QHash will be updated (new objects inserted or old one removed from the list). I have already developed a method to notify the related classes "to remove the pointer" of the used object in them upon removal of that object from the list, but my concern is for the other pointers that are still "alive" in the list, but the memory address have been changed due to the QHash reshuffle.

            QHash<QString, CustomClassObject> Objects;
            
            CustomClassObject* obj = Objects["SECOND"];
            
            SignalClass* signal = new SignalClass(obj);  //This can live up to 30 minutes or more
            
            Objects.RemoveAll("FIRST");
            

            will the obj still point to the correct real list object or I will have a memory corruption?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 1 Jul 2015, 17:38 last edited by
              #6

              I can only repeat myself: "modifying contents of a QHash invalidates (potentially) any pointers pointing into it".

              In short - no, the pointer will (most likely) not point to the correct memory location after that sequence.

              1 Reply Last reply
              1
              • X Offline
                X Offline
                Xumepoc
                wrote on 1 Jul 2015, 17:51 last edited by
                #7

                Sorry I missed that. So how can I overcome this? To relocated a new memory for the new obj instead of just use a pointer to the QHash list? Can I use smart pointers for this?

                T 1 Reply Last reply 1 Jul 2015, 18:09
                0
                • X Xumepoc
                  1 Jul 2015, 17:51

                  Sorry I missed that. So how can I overcome this? To relocated a new memory for the new obj instead of just use a pointer to the QHash list? Can I use smart pointers for this?

                  T Offline
                  T Offline
                  t3685
                  wrote on 1 Jul 2015, 18:09 last edited by
                  #8

                  @Xumepoc

                  If you are using a QHash why can't you just use the key associated to your object to do whatever it is you need to do?

                  1 Reply Last reply
                  1
                  • X Offline
                    X Offline
                    Xumepoc
                    wrote on 1 Jul 2015, 18:18 last edited by
                    #9

                    I will try that also, I will try to pass the container class of the QHash instead of the obj itself.

                    1 Reply Last reply
                    0

                    6/9

                    1 Jul 2015, 17:38

                    • Login

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