Results 1 to 5 of 5

Thread: Passing a pointer in Signal/Slot Connection

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Passing a pointer in Signal/Slot Connection

    Greetings,

    I am trying to pass a pointer as a parameter using signal/slot connections. I am having problems accessing the data on the slot side. On the 2nd or 3rd access the app crashes.

    I have a number of other successful connections between these 2 classes already but none attempt to pass a pointer as a parameter. It would seem to be a simple thing to pass a void*. Is there a particular way to pass a void* using signal/slot connections?

    Does anyone see what I am doing wrong?

    Using Qt 4.2.2 and MSVC 2005.

    Here are some code snippets...

    Qt Code:
    1. // GetProperty Map
    2. typedef struct deviceData
    3. {
    4. uint nCounter; // property counter
    5. void* pData; // device data pointer
    6.  
    7. } DevData;
    8.  
    9. typedef std::map<unsigned long, DevData*> GetPropertyMap;
    10. typedef GetPropertyMap::iterator GetPropertyMapIter;
    11. typedef GetPropertyMap::value_type GetPropertyEntry;
    12.  
    13. GetPropertyMap m_PropMap;
    14.  
    15. // Signal declaration
    16. void allDMXPropsReceived( unsigned long member, void* pData );
    17.  
    18.  
    19. // Create structure
    20. DevData* pDevData = new DevData();
    21. pDevData->nCounter = nPropTotal;
    22. pDevData->pData = static_cast<void*>( pNode );
    23.  
    24. m_PropMap.insert( GetPropertyEntry( handle, pDevData ) );
    25. .
    26. .
    27. .
    28. // Retreive the data pointer from the struct in the map
    29. GetPropertyMapIter mit = m_PropMap.find( member );
    30. if ( mit != m_PropMap.end() )
    31. {
    32. if ( mit->second != NULL )
    33. {
    34. ...
    35. if ( mit->second->pData != NULL )
    36. emit allDMXPropsReceived( member, mit->second->pData );
    37. ...
    38. }
    39. }
    40.  
    41.  
    42. //Slot declaration
    43. void SLOT_AllDMXPropsReceived( unsigned long u4member, void* pData );
    44.  
    45. // Slot connection
    46. connect( m_pController, SIGNAL( allDMXPropsReceived( unsigned long, void* ) ),
    47. this, SLOT( SLOT_AllDMXPropsReceived( unsigned long, void* ) ), Qt::QueuedConnection );
    48.  
    49.  
    50. void DMXTable::SLOT_AllDMXPropsReceived( unsigned long u4member, void* pData )
    51. {
    52. CDMXGateway* pNode = static_cast<CDMXGateway*>( pData );
    53.  
    54. // Access data & crash
    55. SetDeviceName( pNode->GetDeviceName() );
    56. SetIPAddr( pNode->GetIPAddr() );
    57. ...
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Passing a pointer in Signal/Slot Connection

    maybe you should try dynamic_cast<> and only access the data if the pointer to the casted object is valid. At least, that is the safest way to do a cast.

    Qt Code:
    1. void DMXTable::SLOT_AllDMXPropsReceived( unsigned long u4member, void* pData )
    2. {
    3. CDMXGateway* pNode = dynamic_cast<CDMXGateway*>( pData );
    4. if (pNode) {
    5. // Access data & crash
    6. SetDeviceName( pNode->GetDeviceName() );
    7. SetIPAddr( pNode->GetIPAddr() );
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing a pointer in Signal/Slot Connection

    Quote Originally Posted by momesana View Post
    maybe you should try dynamic_cast<> and only access the data if the pointer to the casted object is valid. At least, that is the safest way to do a cast.

    Qt Code:
    1. void DMXTable::SLOT_AllDMXPropsReceived( unsigned long u4member, void* pData )
    2. {
    3. CDMXGateway* pNode = dynamic_cast<CDMXGateway*>( pData );
    4. if (pNode) {
    5. // Access data & crash
    6. SetDeviceName( pNode->GetDeviceName() );
    7. SetIPAddr( pNode->GetIPAddr() );
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 
    Are you advising him to patch his code?

    @mclark: Could we see the code from the thread where you emit the signal?
    BTW, you shouldn't use void*, but register your custom type with qRegisterMetaType.
    And if you're using visual studio, why in the world won't you debug the application? You can't get a debugger better than that.
    Last edited by marcel; 6th November 2007 at 18:32.

  4. #4
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing a pointer in Signal/Slot Connection

    Thank you for taking the time to look at my code.

    It turns out this is not a Qt issue after all. But, of course, as soon as I post to the forum, the real problem hits me in the face. The pointer is coming across just fine when passing as a void*. The data is accessible in the slot as I expect. All would work if this programmer would not delete the data and then try to use it.

    Again, thanks for your time.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Passing a pointer in Signal/Slot Connection

    Are you advising him to patch his code?
    I thought the crash may have been the result of a pointer pointing to the wrong object (not having the members he tried to call.) or a dangling pointer. In both cases the dynamic_cast would have returned a null pointer. Thus he could have avoided the crashes. But in his case, the object was deleted from outside while he tried to access it, so it wouldn't have helped him to use a dynamic_cast.

Similar Threads

  1. Replies: 1
    Last Post: 4th November 2006, 07:53
  2. queued signal/slot connection with QList<int>
    By smalls in forum Qt Programming
    Replies: 2
    Last Post: 7th February 2006, 15:32

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.