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. QSerialPort: ASSERT failure in QList<T>::operator[]: "index out of range"
Qt 6.11 is out! See what's new in the release blog

QSerialPort: ASSERT failure in QList<T>::operator[]: "index out of range"

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 4.5k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello

    I have an application that must read data from serial port, communicating with mbed device. The QSerialPort is running in separate thread, and the reading of the incoming data happens in this slot:
    @void SerialPortT::readyRead()
    {
    if(port->bytesAvailable()) {
    QByteArray data = port->readAll();

        if(!data.isEmpty()) {
            char *temp = data.data();                                                  
            for(int i = 0; temp[i] != '\0'; i++) {                                      
                switch(STATE) {                                                        
                case WAIT_START:                                                      
                    if(temp[i] == START_MSG) {                                          
                        STATE = IN_MESSAGE;                      
                        break;                                                          
                    }
                    break;
                case IN_MESSAGE:                                                       
                    if(temp[i] == END_MSG) {                                           
                        STATE = WAIT_START;
                        QStringList incomingData = receivedData.split(' ');             
                        emit newData(incomingData);                                     
                        break;
                    }
                    else if(isdigit(temp[i]) || isspace(temp[i]) ) {                                       
                        receivedData.append(temp[i]);
                    }
                    break;
                default: break;
                }
            }
        }
    }
    

    }@

    The program runs for a while and then crashes with the message in the topic. I am commenting the parsing part of the slot for the testing and just displaying the received QByteString, so the parsing is not actually the reason why this happens. I have used assert messages before and after the readAll() method, and it looks that the crash happens while running this method; the after assert message is not printed.

    Thanks

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

      Hi,

      Are you sure that you get a '\0' each time ?

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

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Actually, for the tests I comment out the parsing part and use qDebug() to print out the received byte array, provided bytesAvailable() si greater than zero and the byte array is not empty.
        However, the error happens only when I use the port in a separate thread, When I run the QSerialPort in the main thread, it works fine (at least for an hour).

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

          Since you are using it in another thread, can you show the code ?

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

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Sure, but the code is what I have showed here - it subclasses QThread. I am opening the port in the run() method and then entering the exec() loop; the QSerialPort's readyRead() signal is connected to a slot that does the same as the code I have posted. Here is the run()

            @void SerialPortT::run()
            {
            port = new QSerialPort(portInfo, 0);

            if(port->open(QIODevice::ReadOnly)) {           
                if(! port->setBaudRate(baudRate)) {
                    qDebug() << "Baud Rate Error::" << port->errorString();
                }
                if(!port->setParity(parity)) {
                    qDebug() << "Parity Error:" << port->errorString();
                }
                if(!port->setDataBits(dataBits)) {
                    qDebug() << "Data Bits Error::" << port->errorString();
                }
                if(!port->setStopBits(stopBits)) {
                    qDebug() << "Stop Bits Error:" << port->errorString();
                }
            
                connect(port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(portError(QSerialPort::SerialPortError)));
                connect(port, SIGNAL(readyRead()), this, SLOT(readyRead()));    
                emit portOpenOK();        
                qDebug() << "Port: " << port->portName() << "Mode: " << port->openMode();           
            } else {
                emit portOpenFail();            
            }
            exec&#40;&#41;;                                  
            

            }@

            portInfo is QSerialPlotInfo object. That is actually the whole thread except the constructor and destructor.
            So far my appliation is running fine with QCustomPlot and the QSerialPort in the same thread. I think I will leave it like this.

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

              You should rather implement the worker object paradigm.

              Also, did you run your application using the debugger to see exactly where it fails ?

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

              1 Reply Last reply
              0

              • Login

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