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. index out of range
Forum Updated to NodeBB v4.3 + New Features

index out of range

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 6 Posters 3.0k Views 3 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    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

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

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

      ODБOïO 1 Reply Last reply
      1
      • Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

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

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

        ODБOïO 1 Reply Last reply
        1
        • mrjjM mrjj

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

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #4

          @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

          1 Reply Last reply
          0
          • Pl45m4P Pl45m4

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

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

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

            @Pl45m4 said in index out of range:

            Where else

            nowhere, it is altered only with append()

            mrjjM 1 Reply Last reply
            0
            • ODБOïO ODБOï

              @Pl45m4 said in index out of range:

              Where else

              nowhere, it is altered only with append()

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              ODБOïO 1 Reply Last reply
              1
              • mrjjM mrjj

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

                ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by ODБOï
                #7

                @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

                mrjjM 1 Reply Last reply
                0
                • ODБOïO ODБOï

                  @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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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

                  or
                  items.at(x)

                  ODБOïO 2 Replies Last reply
                  1
                  • mrjjM mrjj

                    @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

                    or
                    items.at(x)

                    ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on last edited by ODБOï
                    #9

                    @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

                    or
                    items.at(x)

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

                    mrjjM 1 Reply Last reply
                    0
                    • ODБOïO ODБOï

                      @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

                      or
                      items.at(x)

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @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 ?

                      ODБOïO 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @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 ?

                        ODБOïO Offline
                        ODБOïO Offline
                        ODБOï
                        wrote on last edited by
                        #11

                        @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

                        ODБOïO 1 Reply Last reply
                        0
                        • ODБOïO ODБOï

                          @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

                          ODБOïO Offline
                          ODБOïO Offline
                          ODБOï
                          wrote on last edited by
                          #12

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

                          mrjjM JonBJ 2 Replies Last reply
                          0
                          • ODБOïO ODБOï

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

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            1
                            • ODБOïO ODБOï

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

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #14

                              @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?

                              ODБOïO 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @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?

                                ODБOïO Offline
                                ODБOïO Offline
                                ODБOï
                                wrote on last edited by ODБOï
                                #15

                                @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 ?

                                JonBJ kshegunovK J.HilkJ 3 Replies Last reply
                                0
                                • ODБOïO ODБOï

                                  @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 ?

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #16

                                  @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.

                                  1 Reply Last reply
                                  1
                                  • ODБOïO ODБOï

                                    @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 ?

                                    kshegunovK Offline
                                    kshegunovK Offline
                                    kshegunov
                                    Moderators
                                    wrote on last edited by
                                    #17

                                    @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).

                                    Read and abide by the Qt Code of Conduct

                                    1 Reply Last reply
                                    1
                                    • ODБOïO ODБOï

                                      @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 ?

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

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

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

                                      F10 on Windows


                                      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
                                      2
                                      • mrjjM mrjj

                                        @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

                                        or
                                        items.at(x)

                                        ODБOïO Offline
                                        ODБOïO Offline
                                        ODБOï
                                        wrote on last edited by ODБOï
                                        #19

                                        hi
                                        @mrjj said in index out of range:

                                        or
                                        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

                                        Pl45m4P 1 Reply Last reply
                                        1
                                        • ODБOïO ODБOï

                                          hi
                                          @mrjj said in index out of range:

                                          or
                                          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

                                          Pl45m4P Offline
                                          Pl45m4P Offline
                                          Pl45m4
                                          wrote on last edited by Pl45m4
                                          #20
                                          This post is deleted!
                                          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