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:
// Do some stuff
QMutexLocker locker(&mMutex); // Error here
mutable QMutex mMutex;
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:
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?
You've corrupted your object and the QMutex member has become invalid. Review your code or valgrind it.
Your own mutex implementation just removes the assertion, but does not change the fact that your code is most probably broken.
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.