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


[Non-text portions of this message have been removed]

Reply via email to