Results 1 to 11 of 11

Thread: Strange bug in the debugger!?

  1. #1
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Strange bug in the debugger!?

    Hi, I have the following code:

    Qt Code:
    1. if (a != b) {
    2. if (c != 0 && a_vec[c-1] == b) {
    3. cout << "a is not longer equal to b" << endl;
    4. }
    5. }
    6. else if (c == 0 || a_vec[c-1] != b) {
    7. cout << "a is now equal to b" << endl;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Here, a is a normal variable, b is a constant and c is an index that starts at 0 and is increased by one between everytime this code runs. I think that I should get each other "a is not longer equal to b" and each other "a is now equal to b" in cout, but everything I get is "a is now equal to b". When I run the debugger, I put breakpoints at rows 1, 2 and 7 in this code. The strange thing is that when the code breaks at row one, I press F10 to see where the program heads next. First, it moves to row 2. When I press it again, it does not enter the if clause and goes to row three, but it jumps directly to row 7! How is this possible? How can it take that strange jump directly from row 2 to row 7? There would be no way it go move there without getting caught at some other break point, unless there is some bug in the debugger, right?

    I'm using Qt Creator 2.3.1 based on Qt 4.7.4 (32 bit). The compiler is MinGW.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Strange bug in the debugger!?

    Qt Code:
    1. if (a != b) {
    2. cout << "if" << endl;
    3. if (c != 0 && a_vec[c-1] == b) {
    4.  
    5. cout << "a is not longer equal to b" << endl;
    6.  
    7. }
    8.  
    9. }
    10.  
    11. else if (c == 0 || a_vec[c-1] != b) {
    12.  
    13. cout << "a is now equal to b" << endl;
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    what happens?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange bug in the debugger!?

    The compiler is probably optimizing your code. Take a look at your compile flags. Even at the lowest optimization, it still might decide to optimize (there is no such thing as non-optimized code really).

    Other than that... debugging output is what you have to work with.

    HTH

  4. #4
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Strange bug in the debugger!?

    I can't seem to be able to repeat the strange behavior when it just printed "a is now equal to b" all the time; now it only prints "a is now equal to b" once in a row, as expected. On the other hand, it still jumps very strangely from row 2 to row 7 in the code when I'm debugging it. It does that everytime the code runs, but it still doesn't print "a is now equal to b" every time. Weird.

    Quote Originally Posted by smacchia View Post
    there is no such thing as non-optimized code really
    But when you are debugging your code, you expect the program pointer to jump between the different lines in your code as expected, at least if you are using the lowest optimization level, right? How do check the compiler flags by the way?

  5. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Strange bug in the debugger!?

    Try to clean and rebuild the project.

    You'll get discrepancies between what you see in the debuger and what your program is actually doing if source files have changed since the time the objects were compiled.
    It can happen when you edit file while the debugger is running or when you mess with header files and changes were not picked up correctly.

  6. #6
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange bug in the debugger!?

    Yes the current line pointer can change like that if the code is optimized in such a way as to make that happen. You'd have to look at and understand the assembly. Checking the compiler flags you're using is done by going into visual studio and looking at the project. On linux, it's by looking at the pro files or the generated makefile.

    It could also be that the test at line 3 is false which automatically puts the line pointer on 7.

  7. #7
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Strange bug in the debugger!?

    I'm not using Visual Studio, I'm using Qt Creator. And there is no test at line 3, only at line 1, 2 and 6.

    I still don't understand how it can jump directly from row 2 two row 7 though. If the test fails, it's still not going to get to line 7 because for that to happen the test at line 1 must have failed, which it has obviously not since I am at line 2. It may be that I need to re-build the project to get rid of some discrepancies that can have occurred. I'll have to try that, thanks for the tip.

  8. #8
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange bug in the debugger!?

    There absolutely is a test at line 3, at least in the example shown here. It's the
    Qt Code:
    1. if (c != 0 && a_vec[c-1] == b)
    To copy to clipboard, switch view to plain text mode 

    line. That's the test. And compilers do all kinds of optimizations. If the test at line 3 failed, then what you're seeing wouldn't be at all unusual.

  9. #9
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Strange bug in the debugger!?

    Quote Originally Posted by Yes View Post
    Hi, I have the following code:

    Qt Code:
    1. if (a != b) {
    2. if (c != 0 && a_vec[c-1] == b) {
    3. cout << "a is not longer equal to b" << endl;
    4. }
    5. }
    6. else if (c == 0 || a_vec[c-1] != b) {
    7. cout << "a is now equal to b" << endl;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by amleto View Post
    Qt Code:
    1. if (a != b) {
    2. cout << "if" << endl;
    3. if (c != 0 && a_vec[c-1] == b) {
    4. cout << "a is not longer equal to b" << endl;
    5. }
    6. }
    7. else if (c == 0 || a_vec[c-1] != b) {
    8. cout << "a is now equal to b" << endl;
    9. }
    To copy to clipboard, switch view to plain text mode 

    what happens?
    Quote Originally Posted by smacchia View Post
    There absolutely is a test at line 3, at least in the example shown here. It's the
    Qt Code:
    1. if (c != 0 && a_vec[c-1] == b)
    To copy to clipboard, switch view to plain text mode 

    line. That's the test. And compilers do all kinds of optimizations. If the test at line 3 failed, then what you're seeing wouldn't be at all unusual.
    I may have confused things with my suggested test. Why he didn't try it, though, I don't know.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  10. #10
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Strange bug in the debugger!?

    I did try the test you suggested, and I got the cout print that you added. The debugger continued to the test on line 3 (now in your example) and then jumped directly to line 8 which says "a is now equal to b". It did that, iteration after iteration, but when I looked at what had actually been printed, it had not written "a is now equal to b" even once (from what I can remember).

    Now I can't compile the program because I get some strange error saying ":-1: error: [ui_mainwindow.h] Error 258" and "File not found" which I don't know what it means (I didn't know that I had such a file), so I can't currently do any more debugging. Thank you for the help I have got in this post, though.

  11. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,478
    Thanked 274 Times in 269 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange bug in the debugger!?

    ui_*.h are files generated by UI compiler from *.ui files. You have in project file mainwindow.ui but it is not compiled. It seems that your project is corrupted.

Similar Threads

  1. cdb as debugger
    By pkj in forum Newbie
    Replies: 3
    Last Post: 27th January 2012, 13:59
  2. Qt Creator Debugger failure
    By Carlton in forum Qt Tools
    Replies: 3
    Last Post: 23rd November 2010, 20:58
  3. Debugger helper
    By JohnToddSr in forum Qt Tools
    Replies: 33
    Last Post: 15th June 2010, 23:19
  4. QT IDE and Debugger
    By onefootswill in forum Newbie
    Replies: 16
    Last Post: 25th July 2008, 20:39
  5. Need Qt IDE with Debugger
    By rajeshs in forum General Discussion
    Replies: 3
    Last Post: 10th October 2007, 12:21

Tags for this Thread

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.