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



  • I am still relatively new to Qt and I have recently been working on a large project. When I attempt to run the project I get this error:

    /ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/arm-linux-gnueabihf/qt5/QtCore/qlist.h, line 487
    The program has unexpectedly finished.
    

    How I might go about tracking down the source of the problem?

    I believe that the addition of this code is causing the error

    startvaluexy = Client::straightxy;
    qDebug() << "start value Received from server :" << startvaluexy;
    QStringList xy = startvaluexy.split("|");
    
    x = xy[2];
    
    QString num1 = x;
    int x = num1.toInt();
    
    qDebug() << "start x value :" << x;
    
    y = xy[3];
    
    QString num2 = y;
    int y = num2.toInt();
    
    qDebug() << "start y value :" << y;
    

    When this x = xy[2]; y = xy[3]; is taken out, then runs fine.



  • What is the real size of your xy list?


  • Moderators

    @Jerwinprabu You should never assume that you get what you expect!
    You expect that this line

    QStringList xy = startvaluexy.split("|");
    

    will generate a list with at least 4 elements. But whether this is the case depends on the content of startvaluexy.
    So, you should check the length of xy before accessing any elements of it!
    "index out of range" - this message actually tells you what is wrong: you're accessing an element in the list which does not exist.



  • @Jerwinprabu Hi,friend,welcome.

    I modified your code snippet.

    startvaluexy = Client::straightxy;
    qDebug() << "start value Received from server :" << startvaluexy;
    QStringList xy = startvaluexy.split("|");
    int xySize = sy.size(); ///< get the xy list size;
    
    if(xySize < 4){
        return false;
    }
    
    bool ok;
    int x = xy[2].toInt(&ok); ///< you should check whether convert success
    if(!ok){
        return false;
    }
    
    int y = xy[3].toInt(&ok);
    if(!ok){
        return false;
    }
    
    // when run here, you will get the correct x,y value
    return true;
    


  • If I sent start command from server, output displayed like this on client side

    WebSocket connected
    message received : "straight|xy|3|0" 
    
    start xy value Received from server : "straight|xy|3|0"
    start x value emitted to Robot : 3
    start y value emitted to Robot: 0
    int of start x value emitted : 3
    int start y value emitted : 0
    

    If I click start button on client side that time only I got the error.

    WebSocket connected
    
    start xy value Received from server : ""
    ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/arm-linux-gnueabihf/qt5/QtCore/qlist.h, line 487
    

  • Moderators

    @Jerwinprabu You see: you're trying to split an empty string, this is not going to work. So please add some check to your code as @joeQ suggested.
    And you should check why you don't get what server sent, but this is another issue.



  • @joeQ When I was try to run the program I got the error

    error: return-statement with a value, in function returning 'void' [-fpermissive]
             return false;
    


  • @Jerwinprabu (⊙o⊙)…

    bool myFunctionName() ///< Notes; function return value type must bool
    {
          /* ... */
          return true;
    }
    

  • Moderators

    @Jerwinprabu @joeQ Or just do

    return;
    

    if you do not use the return value...



  • @jsulm @joeQ Thanks for your help. Ya @joeQ given proper sample. I have tried that. Here I have posted the solution now client side that is working fine, one time I want to check from server side. Kindly check, if anything wrong awaiting for your further response

    startvaluexy = Client::straightxy;
    qDebug() << "start xy value Received from server :" << startvaluexy;
    QStringList xy = startvaluexy.split("|");
    int xySize = xy.size(); 
    qDebug() << "start xy size :" << xySize;
    
    if(xySize > 4){
         x = xy[2];
         y = xy[3];
      }
    
         QString num1 = x;
         int x = num1.toInt();
    
         qDebug() << "start x value  :" << x;
    
         QString num2 = y;
         int y = num2.toInt();
    
         qDebug() << "start y value :" << y;
    
        xstart = x;
        ystart = y;
    

    After checking the size will modify this xySize > 4


  • Moderators

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

    if(xySize > 4){

    shouldn't it be

    if(xySize >= 4){
    

    ?
    And if it is not >= 4 what would be the values of x and y? Shouldn't you just skip processing in this case (do return as @joeQ suggested)?

    What are you trying to do with this

    QString num1 = x;
    int x = num1.toInt();
    

    ?! Why do you assign an integer to a string and then convert this string to integer again? Same for

    QString num2 = y;
    int y = num2.toInt();
    


  • @jsulm from server I will receive the string then I want to convert that string into int. Again I want to convert for sending to server(for map).


  • Moderators

    @Jerwinprabu Then why do you convert it back from string to int? x is already int, what's the point to convert from string to int?
    Also

    QString num1 = x;
    

    is not a conversion to string from int! It will not even compile. If you want to convert int to string then do it like shown here http://doc.qt.io/qt-5/qstring.html#number



  • @jsulm Because I want to take that value to int only xstart, ystart. I want to convert from string to int only. x, y is not int.

    QString x;
    QString y;
    

  • Moderators

    @Jerwinprabu Again

    QString num1 = x;
    int x = num1.toInt();
    

    x already contains the integer value! Now you convert it to string then again to int - it will be same as before.

    int x = 1;
    QString num1 = QString::number(x);
    x = num1.toInt();
    qDebug() << x;
    

    What do you think the last line will print out?
    Shouldn't it be just

    QString num1 = QString::number(x);
    xstart = x;
    

    ?


  • Moderators

    @Jerwinprabu Also here you redefine the variable x

    QString num1 = x;
    int x = num1.toInt();
    

    is there a reason why you're doing this?


  • Moderators

    After chatting with @Jerwinprabu I realised that I misunderstood his code: x and y are actually QString not int.


  • Qt Champions 2017

    @jsulm
    Well x is a pretty confusing name for a string in most cases.
    I was sure also it was int.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.