ASSERT failure in QList<T>::operator[]: "index out of range"
-
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.
-
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.
@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.
-
@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.
@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. -
@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.@shreya_agrawal That is in no way a proper solution (even if it "works") to whatever your issue is....
-
@shreya_agrawal That is in no way a proper solution (even if it "works") to whatever your issue is....
@JonB
Then, is using a stack and creating all the valves prior to starting a QTimer, a proper solution? -
@JonB
Then, is using a stack and creating all the valves prior to starting a QTimer, a proper solution?The proper solution is that you dont access deleted/removed elements or indices in your list
-
@JonB
Then, is using a stack and creating all the valves prior to starting a QTimer, a proper solution?@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[]
orat()
. 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.
-
@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[]
orat()
. 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.
@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. -