Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QByteArray dequeue() crash

QByteArray dequeue() crash

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 588 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abhi_Varma
    wrote on last edited by Abhi_Varma
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Is this a multithreaded application ?
      If so, you need to protect the access to your queue.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        Is this a multithreaded application ?
        If so, you need to protect the access to your queue.

        A Offline
        A Offline
        Abhi_Varma
        wrote on last edited by Abhi_Varma
        #3

        @SGaist Yes. It is multithreaded app. and how can I protect the access to the queue. (I have added few more comments to the post at the end)

        J.HilkJ 1 Reply Last reply
        0
        • A Abhi_Varma

          @SGaist Yes. It is multithreaded app. and how can I protect the access to the queue. (I have added few more comments to the post at the end)

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @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


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved