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. emitting signal causes destructor call
Forum Updated to NodeBB v4.3 + New Features

emitting signal causes destructor call

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    So, I have a program with a worker routine as follows:

    void Worker::run()
    {
        int len;
    
        running.ref(); // set to value of 1
        while (running)
        {
            len = sm.recv(buffIn, sizeof(buffIn));
            if (len >= 0)
            {
                buffIn[len] = '\0';
                Message msg(MSG_UNKNOWN, buffIn);
                emit(newMessage(msg));
            }
            Sleep(10);
        }
        emit reachedEndOfThread();
    }
    

    Message code is:

    Message::Message(MsgType type, char *buffer) : m_type(type)
    {
    .
    .
    .
        hash = new(XmlHash);
    }
    
    Message::~Message()
    {
        delete hash;
    }
    

    The emit in Worker::run() causes the ~Message() to be called twice (creating a seg fault on the 2nd call). Any ideas?

    Thanks...

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      The Message instance is being copied at the emission point because (I assume) your connection is of queued type.

      The way to fix it is to provide a proper copy constructor for the Message class, as the automatically generated one will simply copy the hash pointer and thus make it share the same data among two instances, which will crash on the second one's destructor.

      1 Reply Last reply
      5
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        Oh man...what a rookie mistake.

        As long as I have your attention, how would you recommend implementing the section of the copy constructor that deals with the unordered_map member? I've gotten as far as:

            XmlHash::iterator it;
            hash = new(XmlHash);
            for (it = from.hash->begin(); it != from.hash->end(); ++it)
            {
                
            }
        

        but I'm unsure how to use the iterator to retrieve the hash key. Once I have the key, I can get the value easily enough (I think).

        Thanks...

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

          Hi,

          Since it's an std::unordered_map, the iterator returns a std::pair containing the key and the value.

          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
          2
          • mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

            Hi SGaist -

            Thanks for the refresher on that. I still struggle with the operators for this class.

            I think the easiest way to copy the entire table is:

            Message::Message(const Message &from)
            {
                hash = new(XmlHash);
                hash->insert(from.hash->begin(), from.hash->end());
            }
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Why not use the copy constructor of std::unordered_map ?

              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
              1
              • mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #7

                If hash were simply an unordered_map, I could just do:

                hash = from.hash;
                

                right? But I don't know how to code that when hash is a pointer.

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

                  hash = *otherHashPointer;

                  But why is it a pointer in the first place ?

                  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
                  3
                  • Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    As @SGaist pointed out you could make it a non-pointer. This would spare you implementing a copy constructor and a destructor, as the automatically generated ones would do the right thing.

                    If, however, you want that pointer anyway, then you can just use map's copy constructor:

                    hash = new XmlHash(*from.hash);
                    

                    Btw. This is unrelated but I'm just curious - what's with the weird syntax in new(XmlHash) or emit(newMessage(msg))? What do you need those extra () for?

                    1 Reply Last reply
                    3
                    • mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #10

                      You know, that's a damned good question. It no longer is a pointer. (Of course, now I no longer need the copy constructor, but I'm going to keep it since I struggled so much with it.)

                      Chris: no good reason for the extra parens...they're probably relics of various attempts at getting this working. They too are gone now.

                      Thanks, guys.

                      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