Finding bugs and using call stacks

When you have a bug in your code, Visual Studio halts and allows you to inspect the code. The place at which Visual Studio halts won't always be the exact location of the bug, but it can be close. It will at least be at a line of code that doesn't execute properly.

Getting ready

In this recipe, we'll describe Call Stack, and how to trace where you think an error may come from. Try adding a bug to your code, or add a breakpoint somewhere interesting that you'd like to pause for inspection.

How to do it...

  1. Run the code to a point where a bug occurs by pressing F5, or selecting the Debug | Start Debugging menu option. For example, add these lines of code:
    UObject *o = 0; // Initialize to an illegal null pointer
    o->GetName(); // Try and get the name of the object (has bug)
  2. The code will pause at the second line (o->GetName()).
  3. When the code pauses, navigate to the Call Stack window (Debug | Windows | Call Stack).

How it works…

The Call Stack is a list of function calls that were executed. When a bug occurs, the line on which it occurred is listed at the top of the Call Stack.

How it works…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.3.255