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. ASSERT failure in QList<T>::operator[]: "index out of range"
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 688 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.
  • S Offline
    S Offline
    shreya_agrawal
    wrote on last edited by
    #1

    I basically have a QList, in which I am adding some push buttons (let's call them valves). Also, I am using a QTimer to change the color of all the valves one by one when a certain button is clicked. Now, since all valves don't fit in one page, I have next and previous buttons. What they do basically is, they delete the currently displayed valves, empty the list and then repopulate the QList to display the next or previous valves. When I run this program, I am getting this error: ASSERT failure in QList<T>::operator[]: "index out of range". But this error only occurs sometimes (say, 1 out of 5) after running. I am a newbie at Qt, so any help would be great.

    jsulmJ 1 Reply Last reply
    0
    • S shreya_agrawal

      I basically have a QList, in which I am adding some push buttons (let's call them valves). Also, I am using a QTimer to change the color of all the valves one by one when a certain button is clicked. Now, since all valves don't fit in one page, I have next and previous buttons. What they do basically is, they delete the currently displayed valves, empty the list and then repopulate the QList to display the next or previous valves. When I run this program, I am getting this error: ASSERT failure in QList<T>::operator[]: "index out of range". But this error only occurs sometimes (say, 1 out of 5) after running. I am a newbie at Qt, so any help would be great.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @shreya_agrawal Well, you're trying to access elements in your list which do not exist (so the index is out of range). Debug your application to find out why this happens.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      3
      • jsulmJ jsulm

        @shreya_agrawal Well, you're trying to access elements in your list which do not exist (so the index is out of range). Debug your application to find out why this happens.

        S Offline
        S Offline
        shreya_agrawal
        wrote on last edited by
        #3

        @jsulm
        Thank you for your reply!
        I think the issue might be arising because sometimes the QTimer starts before all the valves are added in the list, hence the 'index out of range' error. I will try using QThread.sleep() and see if it works appropriately.

        JonBJ 1 Reply Last reply
        0
        • S shreya_agrawal

          @jsulm
          Thank you for your reply!
          I think the issue might be arising because sometimes the QTimer starts before all the valves are added in the list, hence the 'index out of range' error. I will try using QThread.sleep() and see if it works appropriately.

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

          @shreya_agrawal That is in no way a proper solution (even if it "works") to whatever your issue is....

          S 1 Reply Last reply
          2
          • JonBJ JonB

            @shreya_agrawal That is in no way a proper solution (even if it "works") to whatever your issue is....

            S Offline
            S Offline
            shreya_agrawal
            wrote on last edited by
            #5

            @JonB
            Then, is using a stack and creating all the valves prior to starting a QTimer, a proper solution?

            Pl45m4P JonBJ 2 Replies Last reply
            0
            • S shreya_agrawal

              @JonB
              Then, is using a stack and creating all the valves prior to starting a QTimer, a proper solution?

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @shreya_agrawal

              The proper solution is that you dont access deleted/removed elements or indices in your list


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              1
              • S shreya_agrawal

                @JonB
                Then, is using a stack and creating all the valves prior to starting a QTimer, a proper solution?

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

                @shreya_agrawal
                Maybe, maybe not, depends on your code, but that is totally different question.

                Your error is that you are passing an index out of range to index a list. You should never do that: if you are lucky it will ASSERT (e.g. if compiled for debug) but if not it could do "anything", behaviour is not defined. If you lucky again you will get some kind of runtime exception (not good), if you are unlucky it will silently return "nonsense".

                What you should do is not assume it will be in range when you cannot be sure, and instead have your code check your index is not less than 0 or greater equal QList::length() before you pass it to [] or at(). If it is out of range you must presumably take some action based on that instead.

                You should code like this instead of relying on the index being "safe" as a result of some stack, timer or whatever state you might (or might not) be in.

                S 1 Reply Last reply
                1
                • JonBJ JonB

                  @shreya_agrawal
                  Maybe, maybe not, depends on your code, but that is totally different question.

                  Your error is that you are passing an index out of range to index a list. You should never do that: if you are lucky it will ASSERT (e.g. if compiled for debug) but if not it could do "anything", behaviour is not defined. If you lucky again you will get some kind of runtime exception (not good), if you are unlucky it will silently return "nonsense".

                  What you should do is not assume it will be in range when you cannot be sure, and instead have your code check your index is not less than 0 or greater equal QList::length() before you pass it to [] or at(). If it is out of range you must presumably take some action based on that instead.

                  You should code like this instead of relying on the index being "safe" as a result of some stack, timer or whatever state you might (or might not) be in.

                  S Offline
                  S Offline
                  shreya_agrawal
                  wrote on last edited by
                  #8

                  @JonB
                  Thank you for the detailed explanation.
                  I will debug the program to make sure that I use the valid indexes while accessing the QList.

                  1 Reply Last reply
                  0
                  • S shreya_agrawal has marked this topic as solved on

                  • Login

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