December 1, 2010

sammarks sammarks
Lab Rat
16 posts

‘index out of range’ When Using QList

Page  
1

Hi, I’m working on a code program called Nova, and I’ve run into an error when launching the program on Ubuntu (or Linux in general I guess)

In the terminal, after compiling, if I type ‘./Nova’, the following error comes up:

ASSERT failure in QList<T>::at: “index out of range”, file ../../../qtsdk-2010.05/qt/include/QtCore/qlist.h, line 455

If I try running the program from within Qt Creator, it crashes.

The program source can be found at http://github.org/NovaEdit/Nova if you need it (It’s too large to post all of it here).

19 replies

December 1, 2010

fcrochik fcrochik
Lab Rat
517 posts

It should not be hard for you to find where on your code you are trying to get an element from a QList with an invalid index. Just set a bunch of breakpoints and start the application on debug mode.

 Signature 

Certified Specialist & Qt Ambassador Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop… Qt everywhere!

December 1, 2010

sammarks sammarks
Lab Rat
16 posts

Alright, let me test and see how that works.

December 1, 2010

IrQX IrQX
Lab Rat
74 posts

I suppose, if you run that code in Debug mode you will see that line, where assert is called.

December 1, 2010

sammarks sammarks
Lab Rat
16 posts

Okay, when stepping through the program, as soon as the .exec() function is run in main.cpp, Qt Creator crashes.

December 1, 2010

sammarks sammarks
Lab Rat
16 posts
IrQX wrote:
I suppose, if you run that code in Debug mode you will see that line, where assert is called.

Could the problem be, because I have three global QLists, and in one function, I do a .insert(index (which is 0), value); into all three QLists.

Could that be causing the problem?

December 1, 2010

sammarks sammarks
Lab Rat
16 posts

Also, I stepped into it further, it seems as though the entire thing crashes when the UI is drawn.

December 1, 2010

sammarks sammarks
Lab Rat
16 posts

I’ve narrowed it down enough to where it happens here:

  1. int returnCode = eventLoop.exec();

Which is in qcoreapplication.cpp within the Qt source files. Line 1009.

December 1, 2010

IrQX IrQX
Lab Rat
74 posts

when program crashes you also could look call stack, which will help you. Not sure, that using global (you means static?) objects is good from modelling point. BTW if your’s QList is initiated and you insert also initiated object in list i don’t see problem here.

Can you show logs?

December 1, 2010

Alexander Kuchumov Alexander Kuchumov
Lab Rat
377 posts

Set breakpoint in file ../../../qtsdk-2010.05/qt/include/QtCore/qlist.h, line 455 and run program

December 1, 2010

Andre Andre
Robot Herder
6296 posts

When I run into these kinds of Qt asserts, I usually do this to make my debugging life easier:

  1. //in file main.cpp
  2. void crashingMessageHandler(QtMsgType type, const char *msg)
  3. {
  4.     switch (type) {
  5.     case QtDebugMsg:
  6.         fprintf(stderr, "Debug: %s\n", msg);
  7.         break;
  8.     case QtWarningMsg:
  9.         fprintf(stderr, "Warning: %s\n", msg);
  10.         break;
  11.     case QtCriticalMsg:
  12.         fprintf(stderr, "Critical: %s\n", msg);
  13.         break;
  14.     case QtFatalMsg:
  15.         fprintf(stderr, "Fatal: %s\n", msg);
  16.         __asm("int3");
  17.         abort();
  18.     }
  19. }
  20.  
  21. int main(int argc, char ** argv)
  22. {
  23.     qInstallMsgHandler(crashingMessageHandler);
  24.   //rest of your main function
  25. }

This will cause a crash when an assert happens (caused by the call on line 16). Now you can use the debugger in Qt Creator to get a call stack. It will be a bit deeper than ideal, but at least you can find the offending call to QList where your index is off.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 1, 2010

Wolf P. Wolf P.
Ant Farmer
354 posts

sammarks wrote:
I’ve narrowed it down enough to where it happens here:

  1. int returnCode = eventLoop.exec();

Which is in qcoreapplication.cpp within the Qt source files. Line 1009.

This is the normal behavior of any runtime error.

I think you should check your calls to QList::at again. Inserting will not cause this sort of assertation failure.

December 1, 2010

Wolf P. Wolf P.
Ant Farmer
354 posts

BTW: Was it really a good idea to ban exceptions from Qt, and using assertations instead?

December 1, 2010

Wolf P. Wolf P.
Ant Farmer
354 posts

Line 90 in mainwindow.cpp seem suspicious to me:

Line 89:

  1. void MainWindow::TextChanged() {
  2.     if (saves.at(ui->tabs->currentIndex() == 1)) {

December 1, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

Andre, interesting debugging practice :)

Wolf P., yes, looks likes someone misplaced ‘)’.

April 13, 2011

sammarks sammarks
Lab Rat
16 posts

Sorry for the extremely long delay in replying, but I discovered a solution to the problem if anyone’s interested.

I discovered that it was because I was using .at() instead of .value() when getting values from the QList. That resolved the problem for me.

Page  
1

  ‹‹ QByteArray error      translator ››

You must log in to post a reply. Not a member yet? Register here!