Results 1 to 17 of 17

Thread: QMultiHash - crashing

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QMultiHash - crashing

    Hi,

    I have the following code :

    Qt Code:
    1. QList<QObject*>Signals;
    2.  
    3. if( theApp->m_dcb.m_oSignalMap.count() > 0 )
    4. {
    5. Signals = theApp->m_dcb.m_oSignalMap.values( strId ) ;
    6. if( Signals.size() > 0 )
    7. {
    8. CDADcb::CSignal* pSignal = (CDADcb::CSignal*)Signals.at(0);
    9.  
    10. // This signal belong to this CAN id, there can be many signals that do
    11. {
    12. // we need to do the conversion here - TODO
    13. pSignal->m_strRawData = strData;
    14. pSignal->m_nCount++;
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    m_oSignalMap is a QMultiHash<QString, QObject*> of which CSignal objects are stored keyed on a string value. For some reason, this bit of code causes my application to crash at incoherent intervals, sometimes after a minute, sometimes after 5. The crash is in QHash.h when trying to find the key ( the key seems to be a bad ptr? ).

    Sorry I can explain more, just seems so hit and miss...

    Regards,
    Steve

  2. #2
    freeskydiver Guest

    Default Re: QMultiHash - crashing

    Qt Code:
    1. QList<QObject*>Signals;
    2.  
    3. if( theApp->m_dcb.m_oSignalMap.count() > 0 )
    4. {
    5. Signals = theApp->m_dcb.m_oSignalMap.values( strId ) ;
    To copy to clipboard, switch view to plain text mode 
    What is the value of strId? It's smaller then m_oSignalMap.count()?
    Qt Code:
    1. // we need to do the conversion here - TODO
    2. pSignal->m_strRawData = strData;
    3. pSignal->m_nCount++;
    To copy to clipboard, switch view to plain text mode 
    It's safe that pSignal isn't NULL?
    Last edited by freeskydiver; 22nd May 2007 at 11:45.

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMultiHash - crashing

    Hi,

    strId is set to "1911" for testing purposes, but sometimes it is a bad ptr and I don't know why?

    Possible because this function gets called from another thread, well I say that, the other thread emits a signal which causes this code to be called, the other thread does pass across a pointer to the following data structure :

    Qt Code:
    1. struct MESSAGEDATA
    2. {
    3. QString strId;
    4. QString strData;
    5. };
    To copy to clipboard, switch view to plain text mode 

    Thread just does :

    Qt Code:
    1. MESSAGEDATA can[MAX];
    2.  
    3. can[0].strId = "1911";
    4. can[0].strData = "0F0F0F0F0F0F";
    5. emit modifytable( &can[0] );
    To copy to clipboard, switch view to plain text mode 

    Quite possible I've answered my own question, it could be the fact that passing a pointer to this structure is not safe?

    And the modifytable slot function is :

    Qt Code:
    1. void CanTree::updatetable( MESSAGEDATA* pData )
    2. {
    3. m_pModel->updateTable( pData->strData, pData->strId );
    4. }
    To copy to clipboard, switch view to plain text mode 

    Which updates the model with the relevant data.

    Regards,
    Steve

  4. #4
    freeskydiver Guest

    Default Re: QMultiHash - crashing

    Qt Code:
    1. void CanTree::updatetable( MESSAGEDATA* pData )
    2. {
    3. try{
    4. m_pModel->updateTable( pData->strData, pData->strId );
    5. }catch(..){
    6. // check the pointer ...or you check him before ;)
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    You can try a TRY CATCH container to better see when the pointer is wrong.

  5. The following user says thank you to freeskydiver for this useful post:

    steg90 (22nd May 2007)

  6. #5
    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: QMultiHash - crashing

    If multiple threads are involved, they are likely to crash your application in a way you describe. Or you keep pointers to unexisting objects in the hash. You might want to use QPointer instead of simple pointers. At least you'll know if objects are still valid.

  7. The following user says thank you to wysota for this useful post:

    steg90 (22nd May 2007)

  8. #6
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMultiHash - crashing

    Hi,

    Got this working now, no crashing, it was because I was passing a pointer to a structure which was created in my thread to another class outside of this thread.

    I'm taking it that it is unwise to pass pointers to objects from one thread to another?

    Regards,
    Steve

  9. #7
    freeskydiver Guest

    Default Re: QMultiHash - crashing

    Quote Originally Posted by steg90 View Post
    Hi,


    I'm taking it that it is unwise to pass pointers to objects from one thread to another?
    You must know what you to doing. It's sometimes no other way for exchange of data.

  10. #8
    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: QMultiHash - crashing

    If those objects are QObjects then it's never safe unless you change the thread affinity at the same time, so that the object is moved to the other thread. But you can't access a QObject from two threads simoultaneously - you have to decide which is going to own the object. And then you can communicate with the object only with events (and signals/slots which use events).

  11. #9
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMultiHash - crashing

    Thanks for that, something to remember in the future.

  12. #10
    freeskydiver Guest

    Default Re: QMultiHash - crashing

    I see a small difference. You can use the same object from different threads. You must have a threadsafe call of the functions. This can you do with QMutex for example. Signals/Slots are very nice, but this is a Qt special. In C++ without Qt, you must communicate over threads, too.

  13. #11
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMultiHash - crashing

    Hi,

    I did try using QMutex around the object in question, but to no avail, it still crashed...

    The mutex will just stop the object from being modified by another thread while one thread is updating it. ( AKA Consumer / Producer problem ).

    Regards,
    Steve

  14. #12
    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: QMultiHash - crashing

    Quote Originally Posted by freeskydiver View Post
    I see a small difference. You can use the same object from different threads. You must have a threadsafe call of the functions. This can you do with QMutex for example. Signals/Slots are very nice, but this is a Qt special. In C++ without Qt, you must communicate over threads, too.
    You can't guard an already existing class with mutexes. It's just not possible. You'd have to wrap all calls in all existing classes that call the object in critical sections. The problem is not that your function is not reentrant (because you are working around this particular problem when putting mutexes into your class) but that the object you call is not thread-safe and using mutexes in your code won't help unless you protect every other code that calls the same object. If you miss at least one, you're just calling for trouble (that's one of the problems with protecting code instead of data, by the way).

    So it doesn't matter whether we're talking about Qt or non-Qt applications. If you don't protect everything, you're not protected at all.

  15. #13
    freeskydiver Guest

    Default Re: QMultiHash - crashing

    Quote Originally Posted by wysota View Post
    You can't guard an already existing class with mutexes. It's just not possible. You'd have to wrap all calls in all existing classes that call the object in critical sections.
    Correct thats the only way, but what should I do? When I must use classes who are not thread safe. Ok it's a dangerous way, but possible.

  16. #14
    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: QMultiHash - crashing

    It's not possible, if you can't overload every method that calls the object! And this is exactly the case.

  17. #15
    freeskydiver Guest

    Default Re: QMultiHash - crashing

    Quote Originally Posted by wysota View Post
    It's not possible, if you can't overload every method that calls the object! And this is exactly the case.
    That's not essential. When I use a class, then I must encapsulate the calls of the not threadsafe class.

  18. #16
    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: QMultiHash - crashing

    Yes, but all calls, not only the ones you make. If you use some kind of architecture (like Qt) it is possible that the architecture calls the objects (like QObjects), which is the exact case here. Thus you can't make QObjects thread-safe just by protecting your code.

  19. #17
    freeskydiver Guest

    Default Re: QMultiHash - crashing

    Upps.... you are right.
    When the system call my object then can it crash.

Similar Threads

  1. Application is Crashing
    By shyam prasad in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2007, 17:04
  2. QSpaceritem problem with crashing
    By moowy in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2006, 19:52
  3. QTableWidget crashing
    By therealjag in forum Qt Programming
    Replies: 5
    Last Post: 19th April 2006, 16:30
  4. QTextCodec decode chinese gb2312 crashing...
    By denny.t in forum Qt Programming
    Replies: 3
    Last Post: 31st March 2006, 06:50

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.