Results 1 to 16 of 16

Thread: Help with out of bounds message

  1. #1
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Help with out of bounds message

    Hi I've got this code that generates some odd behaviour:

    Qt Code:
    1. void function A(){
    2. QString Error = "";
    3. for (int i = 0; i < params.size(); i++){
    4. Error = function B();
    5. qDebug() << "Done B";
    6. }
    7. }
    8.  
    9.  
    10. QString function B(){
    11. //Very long cycle that does many things and calls many functions that might return erros
    12. qDebug() << "Returning B";
    13. return "";
    14. }
    To copy to clipboard, switch view to plain text mode 

    The program crashes and shows this result on the output:

    .
    .
    .
    Returning B
    ASSERT failure in QVector<T>:perator[]: "index out of range", file c:/Qt/QtCreator/qt/include/QtCore/../../src/corelib/tools/qvector.h, line 325
    Done B
    .
    .
    .
    .

    Even after this error the program keeps on executing a while longer (seemingly correctly) before crashing with a Runtime Error with a dialog box from the Microsoft Visual C++ Runtime Library. (I'm using the full QtCreator instalation to write and compile my soft.).

    My thinking is that this QVector error found at some other point in the program and it get written to the console at the point shown. So what I want to know is how I can know exactly in which vector did the error ocurr. I was thinking something along the a try-catch but I don't know how to use them with Qt. And it seems that it's not supported.

    Thanks for anyhelp.

  2. #2
    Join Date
    May 2008
    Location
    USA
    Posts
    22
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with out of bounds message

    In your code what type is params?

    You could try adding
    Qt Code:
    1. CONFIG += debug
    To copy to clipboard, switch view to plain text mode 

    to your .pro file to enable debugging on your program and try to run it from a command prompt and check the output.

    The error that you are getting is related to an index being out of bounds, so it could be a loop that you are doing that is referencing the wrong index for the vector. It could also be that a vector is not being populated as you are expecting.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with out of bounds message

    Are you using multiple threads in your program?

  4. #4
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with out of bounds message

    To answer all questions:

    1) Params is a QVector<MyStruct> Where MyStruct holds a bunch (about six or seven double values). But it's not a problem with this because the size is 12 (I've printed it also) and the error occurs in the first iteration between the index going from 0 to 1. I also use QVectors of different kinds inside the program (where I put the comment).

    2) As I understand it CONFIG += DEBUG builds the project in debug mode (which I'm allready doing) and the output that I show is from the console itself. Windows console will likely show the same messages.

    3) Yes, I use two threads. One has the GUI interface. Since with the push of a button I have to launch a really long process I use a second thread which is where the code written lies. The class that contains the code is a QThread Child.

  5. #5
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with out of bounds message

    Hi:

    So I've been testing out a few things like writing a bit more to the output and so forth a step by step trace. I've have come to the conclusion that the error seems to occur in a destructor. What I really don't understand is why the program keeps on running for two other iterations before stopping. Each iteration is 1000 Steps. So it does 1000 Steps that is when I get the ASSERT error. Then I does another 1000 steps this time NO error is emitted and the it does somewhere between 500 and 800 steps of the next iteration before the error message appears. The number of steps in the third iteration varies with everytime I execute it.

    Any ideas?

    Thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with out of bounds message

    My guess is your problem is related to having two threads. Can we see the code of the worker thread?

  7. #7
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with out of bounds message

    Hi:

    Thanks for the help but I found the problem. There were two threads, right? I'm calling it worker thread and GUI thread. The worker thread sent a couple of QVectors to update a line graph managed by the GUI thread. However I was the one the wrote the code for the graph. And it that code, when the vectors were set to be equal to the internal (plot class) vectors y forgot that I made a search for the highest and lowest value of the vectors in order to do the auto-range feature. However the vectors were empty (which is a valid program condition) but it tried to access them nonetheless without checking if it was empty or not. However since there were two threads the Qt warning appeared in that by coincidence since the graph was set (by user input) to update each 1000 steps just the same amount of steps that the cycle lasted.

    Thanks for all the help.

  8. #8
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with out of bounds message

    hi,

    i understand this thread is pretty much over with, but i have a question for wysota:

    how did u know, before he even told, that there were multiple threads involved?? thats really uncanny!

  9. #9
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with out of bounds message

    I'm going to venture a guess and say that he probably thought of that because of the uncanny place where the QVector message appeared. If there is only one thread a message in that place would have been pretty much impossible.

    But that's just a guess. I'll think I'll let the man answer.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with out of bounds message

    Ok

    Three reasons...
    1. a "strange" delay in the application crashing - that's usually a sign there is another thread butting in
    2. the fact that the crash was occuring (or seemed to occur) during a destructor - destructors shouldn't crash like that. Destructors crashing are often a sign of another thread wreaking havoc in internal structures of some object
    3. having almost 12500 posts in this forum That's really a lot of experience even though some of those posts are trolling around and providing oxygen to flame wars.

    Oh... there is also fourth reason - increasing popularity of multithread solutions.

  11. #11
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with out of bounds message

    Well I'd like then the chance to ask....

    If you need to start a long process (such as a lenghty math computation and by lengthy I mean it lasts more than 10 seconds ) How do you do it without using another thread. Because if you put the process in the button or action or whatever that is suppose to begin said process then your GUI (very understandbly) freezes until the process is over.

  12. #12
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with out of bounds message

    i think THAT's why threads are there..to do sm BIG computation away from a thread where the user is interacting..so using threads is the best bet in such cases. but i guess, we have to be very careful as how we use it and not start abusing them..QT provides us with a very powerful API and its important how we employ it

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with out of bounds message

    Quote Originally Posted by aarelovich View Post
    If you need to start a long process (such as a lenghty math computation and by lengthy I mean it lasts more than 10 seconds ) How do you do it without using another thread. Because if you put the process in the button or action or whatever that is suppose to begin said process then your GUI (very understandbly) freezes until the process is over.
    http://doc.trolltech.com/qq/qq27-responsive-guis.html

  14. #14
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with out of bounds message

    Thans for the help. But according to what I've read, I've actually used the nicest solution to my problem. By using a Worker Thread.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with out of bounds message

    I didn't say you used a wrong solution. I meant there were other solutions as well. Always pick a solution that's tailored to your exact situation - one time this will be a worker thread, another time it will be something else even if at first it may seem both situations are similar.

  16. #16
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with out of bounds message

    No, yes of course. What I meant was that since I'm kind of new at this, I was happy I chose what seemed like the nicest solution.

    Besides I'm not forgetting that page anytime soon.
    Thanks for everything.

Similar Threads

  1. How to get sqlite error message RAISE by trigger
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2008, 11:40
  2. Replies: 4
    Last Post: 12th October 2008, 13:47
  3. How to Compile VTKDesigner2 with Qt?
    By alfredoaal in forum Newbie
    Replies: 0
    Last Post: 5th September 2008, 05:34
  4. treewidget and a message system
    By alisami in forum Qt Programming
    Replies: 7
    Last Post: 27th July 2008, 18:05
  5. statusBar() message color change
    By mclark in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 23:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.