Results 1 to 6 of 6

Thread: Debugging a multi threaded windows app

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Debugging a multi threaded windows app

    Hello there!

    I'm facing a multi threaded windows software that appears to crash randomly. The project is in Eclipse. The general structure is that there is a GUI and a worker thread that generates all kinds of data, which are displayed on the gui. No signals and slots were used. The data structures are global, the worker thread writes into them and the gui thread reads from them. At some point the software crashes always with an illegal use of an [] operator on a QList.

    ASSERT failure in QList<T>:perator[]: "index out of range" ...

    There are lots of QLists and even more [] operators being used.

    Can anyone please give me advice how to debug this? I'm hoping for more than just "use a debugger", because I wouldn't even know where to place a break point. What I need is something like a post mortem stack trace to see where exactly the [] operator failed.

    Thanks
    Cruz

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Debugging a multi threaded windows app

    ASSERT failure in QList<T>:operator[]: "index out of range" ...

    There are lots of QLists and even more [] operators being used.
    ... and no range checks coded into your application ;)
    What I need is something like a post mortem stack trace to see where exactly the [] operator failed.
    Then ask your debugger (yes, the answer is use a debugger) for a backtrace after the program terminates abnormally. Generally, if you run your debugger from inside an IDE this will happen automatically. If you are using the GNU compilers and debugger then "bt" is the magic command ("thread apply" may also be needed). If you are using the Microsoft compiler and cdb then "k" seems to be the magic command.

  3. The following user says thank you to ChrisW67 for this useful post:

    Cruz (10th April 2012)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Debugging a multi threaded windows app

    Alright, thanks, I guess I gonna have to brush up my debugging skills a bit.

    I have noticed in other unsafe multithreaded applications before, that QList seems to increment the size() first, before the last index is actually available.

  5. #4
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Debugging a multi threaded windows app

    QList is not threadsafe that fact that your application hasn't been crashing until now is probably a miracle :P

    The fact that your application is based on a bunch of global variables is really gross (I would expect that kind of solution from a highschool student not a professional). You should consider a major refactor.

    But back to your problem - that error means you are accessing an index that doesn't exist probably due to the different threads modifying the QList at the same time.

    Working with your existing system I would do two things:

    1. Since the datastructures (like QList are not threadsafe) you need to add a QMutex for each of your global data structures, lock the mutex everytime you need to access the datastructure and unlock when you are finished, the best way to do this would be wrap your existing data structures with a class and provide pass through methods for accessing the actual data (this way you can never just forget the mutex)
    2. Always check array index ranges


    for example:
    Qt Code:
    1. class ThreadSafeList
    2. {
    3. void append(int i);
    4. int value(int index);
    5.  
    6. QList<int> mList;
    7. QMutex mMutex;
    8. }
    9.  
    10. void ThreadSafeList::append(int i)
    11. {
    12. mMutex.lock();
    13. mList.append(i);
    14. mMutex.unlock();
    15. }
    16.  
    17. int ThreadSafeList::value(int index)
    18. {
    19. int value = 0;
    20. mMutex.lock();
    21. if(index < mList.count())
    22. {
    23. value = mList.value(index);
    24. }
    25. else
    26. {
    27. qDebug() << "ACCESSING INVALID INDEX";
    28. }
    29. mMutex.unlock();
    30. return value;
    31. }
    To copy to clipboard, switch view to plain text mode 

    **disclaimer this code is only pseudo written off the top of my head for explanation purposes, depending on what you actually have modify it accordingly

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Debugging a multi threaded windows app

    As Berryblue031 points out, if you have two threads accessing the same data structure without any form of locking/access control then you are asking for the application to crash often and unpredictably. This is not the fault of QList. There is a bunch of information in the Qt manuals about thread safety including mechanisms to handle shared data access. .

  7. #6
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Debugging a multi threaded windows app

    Thanks to both of you for your hints and remarks. Trust me, I am fully aware of the problem of thread safety here (or the lack of). The unsafe architecture that I described in my OP is the result of a special requirement. This software is running on a robot. In the first place it has to be fast fast fast and it has to loop with a steady pace of 100Hz. Waiting for mutexes to release or overhead for copying data is something that needs to be avoided at all costs in this special case. That said, having a global and central data structure where one thread writes and another thread reads is not a problem per se, because reading and writing aligned 64 bit data types appears to be atomic on x86 systems. And at the bottom line all sensory input that is being written breaks down to a bunch of floats or doubles. So the worst thing that can happen is that the reading thread is reading a data set that is not entirely in synch, say it has only been half written by the writer thread, which is no problem at all.

    I do realize that using QList in a thread unsafe manner was not a smart choice and leads to the problems I am facing now. However, this problem can be solved, once I can identify the exact place where the problem occurs.

Similar Threads

  1. signal not emitting from run() in multi-threaded app
    By naturalpsychic in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2011, 05:28
  2. Multi-threaded GUI possible?
    By nurtsi in forum Qt Programming
    Replies: 12
    Last Post: 26th November 2010, 21:52
  3. Qstring toStdString() and Multi-threaded DLL (/MD)
    By Daxos in forum Qt Programming
    Replies: 14
    Last Post: 15th May 2010, 11:57
  4. Multi Threaded Client Server application
    By live_07 in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2009, 16:32
  5. QThread - multi threaded signals and slots
    By rishid in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2008, 01:47

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.