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;
        }
    }
KongMD
  • 171
  • 1
  • 4
  • 14
  • 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. – Some programmer dude Mar 31 '13 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. – KongMD Mar 31 '13 at 15:00

1 Answers1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621