Important: Please read the Qt Code of Conduct -

Finding errors

  • Hello everybody
    I wrote a pretty big program in Qt with tens of thousands code lines.
    When I test it, sometimes it gives errors like

    ASSERT failure in QVector<T>::operator[]: "index out of range", file C:\Qt\5.11.0\mingw53_32\include/QtCore/qvector.h, line 436

    The "line 436" information is meaningless to me. Is there a way to find the source of such error without putting qDebug()s everywhere in my code?

  • @Davide87 Hi,

    It means that somewhere in you code to you try to access to an index which does not exists in your QVector.

    You may try to run your application in Debug mode and you have a chance that the application will break at the position containing the issue when your program crash. It depends where is located the issue.

  • Moderators

    @Davide87 Getting a stack trace in a debugger when it fails will tell you where in your code it is. Here is an example showing a backtrace in gdb with the same problem as your code:

    (gdb) r
    Starting program: /home/mike/tmp/vectorbreak/build/breakvector 
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/usr/lib/".
    QVector(0, 1, 2, 3)
    ASSERT failure in QVector<T>::at: "index out of range", file /usr/local/Qt/include/QtCore/qvector.h, line 428
    Program received signal SIGABRT, Aborted.
    0x00007ffff7371d7f in raise () from /usr/lib/
    (gdb) bt
    #0  0x00007ffff7371d7f in raise () from /usr/lib/
    #1  0x00007ffff735c672 in abort () from /usr/lib/
    #2  0x00007ffff78f43cc in QMessageLogger::fatal(char const*, ...) const ()
       from /usr/local/Qt/lib/
    #3  0x00007ffff78f3b14 in qt_assert_x(char const*, char const*, char const*, int) ()
       from /usr/local/Qt/lib/
    #4  0x0000555555555a5f in QVector<int>::at (this=0x7fffffffe288, i=6)
        at /usr/local/Qt/include/QtCore/qvector.h:428
    #5  0x00005555555552c4 in breakVector () at ../main.cpp:9
    #6  0x0000555555555390 in main () at ../main.cpp:17

    Notice #5 .. that is my actual function that calls the invalid index. If you do the same thing with your code you can find the exact line where you have a bad index.

    If you use QtCreator like VRonin suggested then you can just click on the item and it will take you right to the code. On the command line like I did you will have to go to the code yourself. Like in mine it's at "main.cpp:9" which is in main.cpp on line 9.

    If for some reason you can't debug it then just think about whatever you did right before it failed and go look at the code that accesses your vector in the area and you should see what you are indexing wrong. This may need some logs for valid values or a debugger to easily find though.

Log in to reply