PDA

View Full Version : Strange bug in the debugger!?



Yes
20th December 2011, 02:37
Hi, I have the following code:



if (a != b) {
if (c != 0 && a_vec[c-1] == b) {
cout << "a is not longer equal to b" << endl;
}
}
else if (c == 0 || a_vec[c-1] != b) {
cout << "a is now equal to b" << endl;
}


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.

amleto
20th December 2011, 21:45
if (a != b) {
cout << "if" << endl;
if (c != 0 && a_vec[c-1] == b) {

cout << "a is not longer equal to b" << endl;

}

}

else if (c == 0 || a_vec[c-1] != b) {

cout << "a is now equal to b" << endl;

}

what happens?

smacchia
21st December 2011, 01:22
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

Yes
21st December 2011, 14:45
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.


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?

Spitfire
21st December 2011, 15:32
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.

smacchia
21st December 2011, 23:16
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.

Yes
21st December 2011, 23:41
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.

smacchia
22nd December 2011, 19:45
There absolutely is a test at line 3, at least in the example shown here. It's the

if (c != 0 && a_vec[c-1] == b)

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.

amleto
22nd December 2011, 20:03
Hi, I have the following code:



if (a != b) {
if (c != 0 && a_vec[c-1] == b) {
cout << "a is not longer equal to b" << endl;
}
}
else if (c == 0 || a_vec[c-1] != b) {
cout << "a is now equal to b" << endl;
}






if (a != b) {
cout << "if" << endl;
if (c != 0 && a_vec[c-1] == b) {
cout << "a is not longer equal to b" << endl;
}
}
else if (c == 0 || a_vec[c-1] != b) {
cout << "a is now equal to b" << endl;
}

what happens?


There absolutely is a test at line 3, at least in the example shown here. It's the

if (c != 0 && a_vec[c-1] == b)

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.

Yes
26th December 2011, 13:09
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.

Lesiok
26th December 2011, 15:18
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.