Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

index out of range



  • Hi,
    i'm trying to use this functions taken from another qt project to parse GCode file,
    but i get
    ASSERT failure in QList<T>::at: "index out of range", file C:\Qt\5.13.0\msvc2017_64\include\QtCore/qlist.h, line 560

    items is : QList<PosItem> items;

    the original project works well.

    This is how i call

    QString path = qApp->applicationDirPath() + "/ISO.nc";
    preProcessFile(path);
    
        void preProcessFile(QString filepath)
        {
            QFile file(filepath);
            if (file.open(QFile::ReadOnly))
            {
                qDebug()<< "file found";
                items.clear();
    
                float totalLineCount = 0;
                QTextStream code(&file);
                while ((code.atEnd() == false))
                {
                    totalLineCount++;
                    code.readLine();
                }
                if (totalLineCount == 0)
                    totalLineCount = 1;
    
                code.seek(0);
    
                double x = 0;
                double y = 0;
                double i = 0;
                double j = 0;
                bool arc = false;
                bool cw = false;
                bool mm = true;
                int index = 0;
                int g = 0;
    
                bool zeroInsert = false;
                do
                {
                    QString strline = code.readLine();
    
                    index++;
    
                   trimToEnd(strline, '(');
                    trimToEnd(strline, ';');
                    trimToEnd(strline, '%');
    
                    strline = strline.trimmed();
    
                    if (strline.size() == 0)
                    {}//ignore comments
                    else
                    {
                        strline = strline.toUpper();
                        strline.replace("M6", "M06");
                        strline.replace(QRegExp("([A-Z])"), " \\1");
                        strline.replace(QRegExp("\\s+"), " ");
                        //if (strline.contains("G", Qt::CaseInsensitive))
                        {
                            if (processGCode(strline, x, y, i, j, arc, cw, mm, g))
                            {
                                if (!zeroInsert)
                                {
                                    // insert 0,0 position
                                    items.append(PosItem(0, 0, 0, 0, false, false, mm, 0));
                                    zeroInsert = true;
                                }
                                items.append(PosItem(x, y, i, j, arc, cw, mm, index));                         
                            }
                        }
                    }
                } while (code.atEnd() == false);
    
                file.close();
    
           //  setItems(posList);
            }
            else
                printf("Can't open file\n");
        }
    
        bool processGCode(QString inputLine, double& x, double& y, double& i, double& j, bool& arc, bool& cw, bool& mm, int& g)
        {
            QString line = inputLine.toUpper();
    
            QStringList components = line.split(" ", QString::SkipEmptyParts);
            QString s;
            arc = false;
            bool valid = false;
            int nextIsValue = NO_ITEM;
            foreach (s, components)
            {
                if (s.at(0) == 'G')
                {
                    int value = s.mid(1,-1).toInt();
                    if (value >= 0 && value <= 3)
                    {
                        g = value;
                        if (value == 2)
                            cw = true;
                        else if (value == 3)
                            cw = false;
                    }
                    else if (value == 20)
                        mm = false;
                    else if (value == 21)
                        mm = true;
                }
                else if (g >= 0 && g <= 3 && s.at(0) == 'X')
                {
                    x = decodeLineItem(s, X_ITEM, valid, nextIsValue);
                }
                else if (g >= 0 && g <= 3 && s.at(0) == 'Y')
                {
                    y = decodeLineItem(s, Y_ITEM, valid, nextIsValue);
                }
                else if ((g == 2 || g == 3) && s.at(0) == 'I')
                {
                    i = decodeLineItem(s, I_ITEM, arc, nextIsValue);
                }
                else if ((g == 2 || g == 3) && s.at(0) == 'J')
                {
                    j = decodeLineItem(s, J_ITEM, arc, nextIsValue);
                }
                else if (nextIsValue != NO_ITEM)
                {
                    switch (nextIsValue)
                    {
                    case X_ITEM:
                        x = decodeDouble(s, valid);
                        break;
                    case Y_ITEM:
                        y = decodeDouble(s, valid);
                        break;
                    case I_ITEM:
                        i = decodeDouble(s, arc);
                        break;
                    case J_ITEM:
                        j = decodeDouble(s, arc);
                        break;
                    };
                    nextIsValue = NO_ITEM;
                }
            }
    
            return valid;
        }
    
        double decodeLineItem(const QString& item, const int next, bool& valid, int& nextIsValue)
        {
            if (item.size() == 1)
            {
                nextIsValue = next;
                return 0;
            }
            else
            {
                nextIsValue = NO_ITEM;
                return decodeDouble(item.mid(1,-1), valid);
            }
        }
    
        double decodeDouble(QString value, bool& valid)
        {
            if (value.indexOf(QRegExp("^[+-]?[0-9]*\\.?[0-9]*$")) == -1)
                return 0;
            valid = true;
            return value.toDouble();
        }
    

    and PosItem is defined like this :

    #ifndef POSITEM_H
    #define POSITEM_H
    #include <QMetaType>
    //#include "stdio.h"
    #include "definitions.h"
    
    class PosItem
    {
    public:
        PosItem()
            : x(0), y(0), i(0), j(0),
                arc(false), cw(false), mm(true), index(0) {}
        PosItem(double x1, double y1)
            : x(x1), y(y1), i(0), j(0),
                arc(false), cw(false), mm(true), index(0) {}
        PosItem(double x1, double y1, double i1, double j1)
            : x(x1), y(y1), i(i1), j(j1),
                arc(false), cw(false), mm(true), index(0) {}
        PosItem(double x1, double y1, double i1, double j1, bool arc1, bool cw1, bool mm1, int index1)
            : x(x1), y(y1), i(i1), j(j1),
                arc(arc1), cw(cw1), mm(mm1), index(index1) {}
    
        void setCoords(double x1, double y1, double i1, double j1);
        void setCoords(double x1, double y1, bool mm);
        void expand(const PosItem& item);
        double width() { return qAbs(i - x); }
        double height() { return qAbs(j - y); }
        bool isNull() { return x == 0 && y == 0 && i == 0 && j == 0 && arc == false && cw == false && index == 0; }
        void toMm();
        void toInches();
    
    public:
        double x;
        double y;
        double i;
        double j;
        bool arc;
        bool cw;
        bool mm;
        int index;
    };
    
    Q_DECLARE_METATYPE ( PosItem )
    
    #endif // POSITEM_H
    ////////////////////CPP
    
    #include "positem.h"
    
    void PosItem::setCoords(double x1, double y1, double i1, double j1)
    {
        x = x1;
        y = y1;
        i = i1;
        j = j1;
    }
    
    void PosItem::setCoords(double x1, double y1, bool mm1)
    {
        x = x1;
        y = y1;
        i = x1;
        j = y1;
        mm = mm1;
    }
    
    void PosItem::expand(const PosItem& item)
    {
        if (item.x < x)
            x = item.x;
        if (item.y < y)
            y = item.y;
        if (item.i > i)
            i = item.i;
        if (item.j > j)
            j = item.j;
    }
    
    void PosItem::toMm()
    {
        x *= MM_IN_AN_INCH;
        y *= MM_IN_AN_INCH;
        i *= MM_IN_AN_INCH;
        j *= MM_IN_AN_INCH;
        mm = true;
    }
    
    void PosItem::toInches()
    {
        x /= MM_IN_AN_INCH;
        y /= MM_IN_AN_INCH;
        i /= MM_IN_AN_INCH;
        j /= MM_IN_AN_INCH;
        mm = false;
    }
    
    

    how i can resolve this please ? maybe the code.seek(0); is causing this erros ? i tryed to put breakpoints and run in debug mod bug it crashes before running


  • Lifetime Qt Champion

    Hi
    If you put break point in main.cpp at first line in main, it
    never comes there but crashes before ?



  • By calling items.append(....) your QList can not go out of index range.

    Where else do you use the QList<PosItem> items?



  • @mrjj said in index out of range:

    at first line in main

    i just putted one in main.cpp but yes, it still craches.

    My main window pops but another window pops directly saying :

    Microsoft Visual C++ Runtime Library
    Debug error !
    Program : c:\Qt\5.13.0\msvc2017_64\bin\QtCored.dll
    Module : 5.13.0
    File C:\5.13.0\msvc2017_64\include\QtCore/qlist.h
    Line ! 560

    ASSERT failure in QList<T>::at: "index out of range" , file
    c:\QT\5.13.0\msvc2017_64\includeQtCore/qlist.h, line 560



  • @Pl45m4 said in index out of range:

    Where else

    nowhere, it is altered only with append()


  • Lifetime Qt Champion

    @LeLev
    "My main window pops"
    meaning it will enter the main, and then crash after your mainwin is created ?



  • @mrjj said in index out of range:

    rash after your mainwin is created ?

    it is almost at the same time, when i hit Run button , i can see my mainwindow + the error window appearing at the same time

    https://postimg.cc/w1251HXT


  • Lifetime Qt Champion

    @LeLev
    well im asking if you hit f5 debug and have break point at the main.cpp first line.
    (in main function)
    if it goes there as else its a global var that does it.

    You can also try on the
    QList<PosItem> items;
    right click on items and ask where it being used and see if any does

    items.at(x)



  • @mrjj said in index out of range:

    well im asking if you hit f5 debug and have break point at the main.cpp first line.

    Yes i'm running in DEBUG (F5) and i have break point in first line of my main()

    @mrjj said in index out of range:

    right click on items and ask where it being used and see if any does

    items.at(x)

    items is used exclusively in void preProcessFile(QString filepath) function


  • Lifetime Qt Champion

    @LeLev said in index out of range:

    preProcessFile

    Then cant you set break point where you call
    preProcessFile(xx) and single step with f10 F11 into it to see what line that makes it crash ?



  • @mrjj said in index out of range:

    cant you

    i want to, but my app crashes as soon i hit f5 or start in debug mod btn

    https://postimg.cc/w1251HXT



  • for some reason if i change my compiler from msvc to minGW i can now run in DEBUG mod !


  • Lifetime Qt Champion

    @LeLev
    Ok, could be something wrong with the visual studio install and the crash is not related to
    your code as such.



  • @LeLev

    for some reason if i change my compiler from msvc to minGW i can now run in DEBUG mod !

    For whatever your Qt project is, are you saying you can & do freely switch compilers between MSVC & minGW? So you have Qt, and whatever other libraries you use, available for both and it doesn't matter to you which compiler you use? You shouldn't be switching compilers just to magically get your debugging to work right now.

    Surely, you need to decide on one compiler or the other (and being able to debug always reliably is vital) and then do all your development with that?



  • @JonB said in index out of range:

    For whatever your Qt project is, are you saying you can & do freely switch compilers between MSVC & minGW?

    @JonB said in index out of range:

    So you have Qt, and whatever other libraries you use, available for both and it doesn't matter to you which compiler you use?

    yes I am my own boss and I do what I want, i can recompile my libs with needed compiler

    @JonB said in index out of range:

    You shouldn't be switching compilers just to magically get your debugging to work right now.

    why not ? you have 2 'equivalent tools' one is broken, what you do ?

    @JonB said in index out of range:

    Surely, you need to decide on one compiler or the other (and being able to debug always reliably is vital) and then do all your development with that?

    yes ?



  • @LeLev

    why not ? you have 2 'equivalent tools' one is broken, what you do ?

    Permanently change over to the one which is not broken, chuck the other one away, and don't look back.

    Your call, of course.


  • Qt Champions 2017

    @LeLev said in index out of range:

    why not ? you have 2 'equivalent tools' one is broken, what you do ?

    I've personally found 2 bugs in MSVC and about that many in gcc (one of which I even reported), however I'm pretty sure neither is "broken". Please provide a stack trace from the crash (and yes the error is in the user code, everything QList is inlined).


  • Moderators

    @LeLev have you tried start and break on main option ?

    0_1563515176092_b37715d1-3e8f-43eb-90e4-96de46beb3c2-image.png

    F10 on Windows



  • hi
    @mrjj said in index out of range:

    items.at(x)

    sorry i said i never do this calls,but in the lib itself there is that call. Just after closing the file writePath() is called:

    void RenderItemList::writePath(QPainter& painter, bool updatedFromFile)
    {
        QPainterPath path;
        ItemToBase *item = list.at(0); // << crash here
        item->setParams(scale, windowSize.height(), offsetx, offsety);
    

    erros

    #  else
        RaiseFailFastException(nullptr, nullptr, 0);
    #  endif
     qt_message_fatal(QtFatalMsg, context, message);
    { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range");
    

    i saw ind debug mod that list was empty .. i initialised it after reading the file, now everything works correctly.

    thank you for help



  • This post is deleted!

Log in to reply