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:
- 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)
- Always check array index ranges
for example:
class ThreadSafeList
{
void append(int i);
int value(int index);
QList<int> mList;
}
void ThreadSafeList::append(int i)
{
mMutex.lock();
mList.append(i);
mMutex.unlock();
}
int ThreadSafeList::value(int index)
{
int value = 0;
mMutex.lock();
if(index < mList.count())
{
value = mList.value(index);
}
else
{
qDebug() << "ACCESSING INVALID INDEX";
}
mMutex.unlock();
return value;
}
class ThreadSafeList
{
void append(int i);
int value(int index);
QList<int> mList;
QMutex mMutex;
}
void ThreadSafeList::append(int i)
{
mMutex.lock();
mList.append(i);
mMutex.unlock();
}
int ThreadSafeList::value(int index)
{
int value = 0;
mMutex.lock();
if(index < mList.count())
{
value = mList.value(index);
}
else
{
qDebug() << "ACCESSING INVALID INDEX";
}
mMutex.unlock();
return value;
}
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
Bookmarks