A very weird case just happened to me – while using the debugger, it shows me that the value of some boolean variable is true, but when I print it (or do with it any other operation), it behaves as 0 (i.e. false).
What can I do to fix this bug? I’m afraid that this is environmental bug so publishing code-example will be meaningless. (a hidden, annoying memory management bug can’t be the reason, right?), and in this context I want point out that it will surprise me to discover that my environment is not well-configured (I work on this project in this environment for more than a year).
So far I:
- Double-checked that the compiler doesn’t make any optimization to the code, in my project properties.
- Tried to re-open visual studio and clean and re-build the project
- Searched on the web for solutions (didn’t find any).
I use visual studio 2010 and my programming language is C++
Regarding code-sharing requests:
I’m sorry but I can’t publish the code (my bosses will not be happy to see it run in the web)….If you are able to give some ideas about possible reasons to the problem it will be great, and I will look for clues in the code myself, to check if these reasons really caused the problem. However, just to be clear, here are the few code-lines that struggle me:
bool dir = getNode(location)->getNext()->getDirection(); //dir is displayed as "true" in the debbuger
int toPush = (dir == 1) ? 1 : 0; //"toPush" is displayed as "0" in the debbugger
cout<<dir<<endl; //both output 0.
cout<<(dir == true)<<endl;
Following your request I’m attaching a screen-shot. Notice the value of “dir” that is deqplayed at the right-buttom of the screen as “true”, and to the program output on the right, that ends with 0 (which correspond to the “cout<< dir” command).
You shouldn’t use the
==operator to test aboolvalue for truth. Any non-zero value is true. You have twocoutlines and the last two lines in the console window say 240 and 0. I wrote this to demonstrate what I think is happening:A similar thing happens with
dir == truewhere it is probably again testing for the value of one. I have no idea whydiris getting an unusual value in your code (240), but if you remove the comparisons and just test the value (astoPush2above) it should fix the issue.I know you said the
toPushline is just to demonstrate the issue, but are you doing comparisons in any of your real code? If so, remove them.*
dirmight not be 255, depends on the size ofboolin your environment.