0

I set a breakpoint inside a conditional statement that checks for a certain value of a custom datatype. The game will break, but the line it breaks on is completely outside of my breakpoint's scope. Watch variables reveal that it just breaks the first time that loop is iterated through, rendering my debugging conditional statement absolutely useless.

In Visual Studio, the debugger would respect scope, and placing a breakpoint inside a conditional statement would only stop the game if that conditional evaluated to true. Why is this not the case with the GDB debugger in CodeBlocks? Is it because I'm using GDB in Windows? Here's the code:

for(int j = 0 ; j < r->components[i].size() ; j++)
    {
        itype_id type = r->components[i][j].type;
        int req = r->components[i][j].count;

        //DEBUGGING ONLY!!!!!!!!!
        if(type == itm_coffee_raw)
        {
            int pleaseStop = 0;
            if(pleaseStop == 0) //BREAKPOINT IS ON THIS LINE
                bool dontstoptillyougetenough = true;
        }

        if (itypes[type]->count_by_charges() && req > 0) //GAME BREAKS HERE
        {
            if (crafting_inv.has_charges(type, req))
            {
                has_comp = true;
                break;
            }
        }
        else if (crafting_inv.has_amount(type, abs(req)))
        {
            has_comp = true;
            break;
        }
    }
2
  • Do you have any optimization enabled? Those statements inside the if do nothing so the compiler may discard them in an optimization pass. This means that the statements will not exist in the actual executable, and so you can't put breakpoints on them. Commented Mar 31, 2013 at 14:35
  • @JoachimPileborg Yes, I think that was the problem. After removing optimization flags, it behaved normally. Thanks! If you change your comment to a solution, I'll mark it as the solution. Commented Mar 31, 2013 at 15:00

1 Answer 1

1

The code inside the if body doesn't really do anything, so the compiler could see it as "dead code" and remove it from the executable in an optimization pass. This means that the code in question doesn't actually exist in the final executable, and so you can't put a breakpoint there.

Turn off optimizations (always good when debugging in general) and it should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.