Results 1 to 2 of 2

Thread: qRegisterMetaType: getting garbage through signal/slot

  1. #1
    Join Date
    Jul 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy qRegisterMetaType: getting garbage through signal/slot

    I'm using the libipq library to do some packet management on a linux system. When I receive the packet, I successfully use the library to store important information and deconstruct it into its parts. However, now I want to add a separate NetworkStatistics thread, which logs network traffic information. I would like to send pointers to these packets through the signals/slots to the NetworkStatistics thread from my backend thread.

    Originally, I was having the trouble "Cannot queue arguments of type const ipq_packet_msg_t&". I solved this problem by calling qRegisterMetaType<ipq_packet_msg_t>;("ipq_packet_m sg_t"); right above my QObject::connect() statements. Now, however, when I send this const ipq_packet_msg_t& pointer through the signal/slot, I get garbage out the other side. Where my source address might be 127.0.0.1 inside of my backend thread, that same source address right after the signal call inside of NetworkStatistics will be some other random set of 4 integers.

    How can I pass this type through signals/slots?

    Some Code:
    Qt Code:
    1. //REGISTERING WITH QREGISTERMETATYPE:
    2. //Register the ipq_packet_msg_t type with QMetaType, so that
    3. //it can be successfully passed through signals/slots
    4. qRegisterMetaType<ipq_packet_msg_t>("ipq_packet_msg_t");
    5.  
    6. //Connect to NetworkStat
    7. QObject::connect(back, SIGNAL(update_stats_sig(const ipq_packet_msg_t&)), netStat, SLOT(update_stats_slot(const ipq_packet_msg_t&)));
    8.  
    9. //CODE INSIDE BACKEND:
    10. ipq_packet_msg_t *m = ipq_get_packet(buf);
    11.  
    12. emit update_stats_sig(*m);
    13.  
    14. IPAddress sourceIP = getSourceIP(*m);
    15. QString qstr = toIpString(sourceIP);
    16. std::string str = qstr.toStdString();
    17. cout << "Correct IP: " << str << endl; //GIVES CORRECT IP
    18.  
    19. //CODE INSIDE NETWORKSTATISTICS:
    20. //The Slot:
    21. void NetworkStat::update_stats_slot(const ipq_packet_msg_t& m)
    22. {
    23. update(m);
    24. }
    25.  
    26. //The Function:
    27. iphdr* iph = (iphdr*) msg.payload;
    28. unsigned int sourceAddress = ntohl(iph->saddr);
    29.  
    30. QString qstr = toIpString(sourceAddress);
    31. std::string str = qstr.toStdString();
    32. cout << "Source Address inside NetStat: " << str << endl; //GARBAGE SOURCE IP
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qRegisterMetaType: getting garbage through signal/slot

    Never emit pointers (or non-const references) across threads, they might become invalid before the receiving thread has a chance to process them. Make a copy and send the copy instead.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. What if not Signal/Slot
    By munna in forum General Discussion
    Replies: 2
    Last Post: 26th May 2006, 06:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.