QByteArray dequeue() crash
-
Hi, I'm new to qt. I'm facing a small issue with dequeue operation of qt for the following scenario. I have incoming messages coming from server, whenever a new message is sent the following method is invoked and the new message is enqueued to a QQueue<QByteArray> Object called "qbaServerMessages".
void OnMessageReceieved(const QByteArray& baServerMessage) { if(!baServerMessage.isEmpty()) { qbaServerMessages.enqueue(baServerMessage); } }
In the following method I dequeue the "qbaServerMessages" and handle each message separately.
void HandleServerMessages() { while(!qbaServerMessages.isEmpty()) { try { QByteArray baMessage = qbaServerMessages.dequeue(); //....... //Code to handle the message } catch(...) { //Code to handle exception. } } }
I'm running this code on vscode. Once in a while its showing a crash at QByteArray.erase(). When the crash happened here the call stack was like this.
QList::erase(); QList::removeFirst() QList<T>::takeFirst() QQueue::dequeue() void HandleServerMessages()
it was crashing at erase() in qlist. My question why isn't my exception handling working and how can I resolve this.
-
Hi,
Is this a multithreaded application ?
If so, you need to protect the access to your queue. -
@Abhi_Varma said in QByteArray dequeue() crash:
@SGaist Yes. It is multithreaded app. and how can I protect the access to the queue.
If you're unsure about this, maybe you shouldn't do multi threading to start with, especially as Qt-Classes/Modules have almost always asynchronous apis.
For completeness however take a look at mutexes
https://doc.qt.io/qt-5/qmutex.html
https://en.cppreference.com/w/cpp/thread/mutex(I have added few more comments to the post at the end)
My question why isn't my exception handling working and how can I resolve this.
because c++ is not js or python, you can't move everything in a try catch block and be done with it. That only works when exceptions are actually thrown. 😉
An invalid pointer/instance access does not fall in that category :D