QMutex pointer is misaligned
-
I have a centralized buffer class which is being read and written to from a number of different threads. A simple version of my buffer class looks as follows:
@MyBuffer
{
public:void function1()
{
QMutexLocker locker(&mMutex);
// Do some stuff
}int size()
{
QMutexLocker locker(&mMutex); // Error here
return mData->size();
}private:
mutable QMutex mMutex;
QByteArray mData;
}@Everything works fine, except that I always get the following error:
@ASSERT failure in QMutexLocker: "QMutex pointer is misaligned", file /usr/local/Qt-5.0.0/include/QtCore/qmutex.h, line 117@
on the line marked with “// Error here” in the code above.
What is happening here? How can I fix it? -
See the last reply in this thread:
http://stackoverflow.com/questions/9458664/qthread-assert-failure-in-qmutexlocker-qmutex-pointer-is-misaligned
Maybe helps... -
Yip, read that post before. The problem is I have a very large system. I know systems should be leak-free, but it is near impossible to find all memory leaks in the system I'm working on. Isn't there any way I can track the problem more closely?
-
[quote author="goocreations" date="1349793115"]Yip, read that post before. The problem is I have a very large system. I know systems should be leak-free, but it is near impossible to find all memory leaks in the system I'm working on. Isn't there any way I can track the problem more closely?[/quote]
You can try with gprof or valgrind.
-
So this is the only reason why this failure occurs?
-
I've now written my own Mutex and MutexLocker classes based on pthread (pthread_mutex_t) and I'm not getting any kind of locking problems. So why is this such an issue with QMutex?
-
Lukas, what do you mean by "corrupted your object"? Is that the object where I lock the mutex, or is it the mutex object itself?
-
You've written to a memory region you are not allowed to, most probably due to a buffer underrun or a dangling pointer, which leads to a corrupted MyBuffer and in further consequence QMutex object.
It should be actually quite easy to visualize. Place a breakpoint at the QMutexLocker constructor. If the address for <code>&mMutex</code> varies between consecutive calls you've corrupted your memory somewhere.
You might be able to trace the problem with some properly placed data breakpoints (for example on <code>*this</code>) or, as mentioned, valgrind it.