Ananth wrote:
> This was pretty interesting.
> I was debugging a Release version with Optimization enabled in Visual
> Studio.
> This is the simplified code:
>
> bool IsValid()
> {
> bool bReturnValue = false;
>
> if ( /*Some Check*/ )
> bReturnValue = true;
>
> return bReturnValue;
> }
>
> void foo()
> {
> .
> .
> .
> bool bIsValid = IsValid();
> const SomeClass & crSomeThing = (bIsValid) ? SomeClassObjA :
> SomeClassObjB;
> //Mis-leading: bIsValid is always false, even when IsValid() returns
> true.
> //bIsValid is false, but "crSomething" refers to "SomeClassObjA"
> .
> .
> }
>
>
> It took me some time to figure out it can only be because MSVC Compiler has
> optimized away "bIsValid" variable.
> So debugger shows bIsValid as false even though function returned true.
>
> It could be confusing and a mystery to innocent (forgetting one would have
> debugged in Debug mode first)
>
> Any thoughts? Please share.
>
> Thanks
> - Ananth
Using the debugger in Release mode is not always accurate. It jumps
around a lot because of compiler optimizations. Additionally, variable
values are also not always accurate as you have discovered.
That's the whole point of Debug mode: You want to debug the
application, so the compiler doesn't optimize anything.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/