0% found this document useful (0 votes)
74 views39 pages

Debugging in Visual Studio

How to debug Visual C++ using VS. The version of visual studio is older but the techniques are transferable to all versions.

Uploaded by

silly_rabbitz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
74 views39 pages

Debugging in Visual Studio

How to debug Visual C++ using VS. The version of visual studio is older but the techniques are transferable to all versions.

Uploaded by

silly_rabbitz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 39

Debugging in Visual Studio

Learn how to use the powerful integrated debugging


environment provided in Visual Studio 2003 and 2005

By Steve Jones
Game Institute

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


What we will cover

•Debugging native, 32-bit


console and win32
applications
•Learn most common
debugging tools

•Visual Studio .NET 2003 and


2005 IDE environments
•The most common techniques
for debugging
•Examples

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Debugging
What are you trying to find and fix?
• Two main types of code
errors
– Syntax
• Compiler catches most if
not all of these for you.
– Semantic or logical
• Syntactically correct yet
program may “crash and For example: Compiler will not catch an
burn” at run-time! un-initialized pointer but you WILL get
a run-time error if you try to use it!

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Why Should I Use Visual Studio to
Debug my Program?
• Even most experienced coder creates errors or “bugs”
• Visual Studio debugger will provide two powerful run-
time facilities:
– Trace the program Execution
– Watch variables during program execution
• These allow you to stop at procedure locations, inspect
memory and register values, change variables, observe
message traffic, and get a close look at what your code
does.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Project Configuration Settings

• Debug vs. Release Configurations


– The Debug configuration of your program is compiled with full
symbolic debug information and no optimization.
– The Release configuration of your program is fully optimized and
contains no symbolic debug information.
– Must be in Debug configuration to debug your program.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Getting Acquainted with Visual Studio
Debugger
• Debugger Windows
– Autos • Execution Control
– Locals – Starting or Continuing
– Watch Execution
– Call Stack – Stopping
– Command Window – Breaking Execution
– QuickWatch Dialog – Stepping Into and Out of code
– Breakpoints window – Jumping to another location
– Threads
– Modules
– Processes
– Memory
– Disassembly
– Registers

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Debugging Example #1
Console app

This simple console program


should determine whether two
integers are equal.

Code compiled just fine,


0 warnings, 0 errors

… BUT the code obviously has a


logical error! 3 does not equal 5!

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Debugging Example #1 (a console app.)

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


What is a Breakpoint?

• Breakpoints are user-defined


code locations that pause
execution
• You know them by the little,
red “dot” in the left margin of
the editor window
• F9 to add or remove (toggle)
• Or left-mouse click in margin

• Unlimited number of them to


use.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Debugging Example #1 (continued)

Add a breakpoint here


We arbitrarily picked this line because it
seemed like a reasonable place

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Starting the Debugging Session

• Make sure you are in a Debug configuration


• Press F5
• Or click on Debug icon
• Or select menu Debug – Start Debugging

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Debugging Example #1 - Running in the debugger

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Debugging Example #1 Stepping, examine variables

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Execution Control
Stepping through your code

• Starting / Stopping
• Breaking
• Stepping through your
application
F11 F10 Shift + F11
– (F10, F11 or Toolbar Step Into Step Over Step Out
buttons)
• Run to a specific location Equivalent hot-keys
– Run To Cursor (right-click menu)

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Autos Window
• Name
– The names of all variables
in the current statement
and the previous
statement. The current
statement is the statement
at the current execution
location, which is the
statement that will be
executed next if execution
continues.
• Value
– The value contained by each
variable. By default, integer
variables are represented in
decimal form.
• Type
– The data type of each variable
listed in the Name column.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Locals Window
• Name
– This column contains
the names of all local
variables in the current
scope.
• Value
– The value contained by
each variable. By default,
integer variables are
represented in decimal
form.
• Type
– The data type of each
variable listed in the Name
column.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Watch window(s)
• Watch window displays Name, Value, and Type of variables
• Type in or click-drag variables into window
• Change values live while at break
• You have 4 independent Watch windows

(VS 2005 & VC++ Express)

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Debugging Example #1 - Found error

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Debugging Example #1 - Fixed error, recompiled, run, step

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Debugging Example #1 - Step. Hey the code worked!

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


The Call Stack
• Call stack window displays each function name in the order they were
called.
• Yellow arrow identifies the stack frame where the execution pointer is
located
• Double-clicking on a function name takes you to the function in source code
• Click Debug – Windows – Call Stack to show window (if hidden). It is shown
by default.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Example #2
How to Use Conditional Breakpoints

These are breakpoints


that only “break” based
on a specific condition.
In this example, we will
put a conditional
breakpoint in the “for”
loop and the breakpoint
will only stop when our
condition is met.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Example #2
Add a breakpoint with a condition

Let’s say you want to


break execution only
when a condition is
met rather than
break each time the
loop cycles.

1. Add a breakpoint on
the line you’re
interested in.

Then we’ll configure a


condition to it.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Example #2
Breakpoint Properties (VS 2003)

2. Right-mouse click on the


breakpoint
3. Select Breakpoint
Properties…

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Example #2
Open Breakpoint Properties Dialog (VS 2003)

4. The breakpoint dialog


will open.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Example #2
Set hit count condition (VS 2003)
5. Click on Hit Count… button
Select frequency of the break
Default is “break always”

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Example #2
Set a condition (VS 2003)

6. Click on Condition… to set


the criteria for breaking.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Example #2
Set hit count condition (VS 2005 & V C++ Express 2005)

• Right-mouse click on the


breakpoint
• Select Hit Count…

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Example #2
Set a condition (VS 2005 & V C++ Express 2005)

• Right-mouse click on the


breakpoint
• Select Condition…

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Example #2
Result - Code breaks at the desired condition

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Memory Leaks!
How do you know you have them?

• Basic project setup to detect them


• We will use the C Run-Time library
• After building and running the program, the output
window will display any memory leaks.
• We can call another function to force a breakpoint when
the suspect memory is allocated.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Memory Leaks!
Using some C Run-
Run-Time Functions

_CrtDumpMemoryLeaks()
Performs leak checking where called. You want to place this call at all
possible exits of your app.

_CrtSetDbgFlag ()
Sets debugging flags for the C run-time library.

_CrtSetDbgFlag () flag What it does


_CRTDBG_REPORT_FLAG Gets current flag(s)
_CRTDBG_LEAK_CHECK_DF Perform automatic leak checking
at program exit through a call to
_CrtDumpMemoryLeaks

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Example #3 Setting up for detection for Console or
Memory Leaks Win32
“Hook” into the C Run-time libraries to use the debug heap
1. Include the following lines in your program as the basics.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Memory Leaks
_CRTDBG_MAP_ALLOC_

• Including crtdbg.h, you map the malloc and free functions to their
Debug versions, _malloc_dbg and _free_dbg, which keep track of
memory allocation and deallocation
• Without #define _CRTDBG_MAP_ALLOC:
• Memory allocation number (inside curly braces)
• Block type (normal, client or CRT)
• Memory location in hex
• Size of block in bytes
• Contents of the first 16 bytes in hex
• With it defined you get all the above plus:
• File name
• Line number

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Memory Leaks
Output window dump

Line
Source file Memory Block Memory Block
number
where leak allocation type location size
within
occurred number
source file

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Memory Leaks
Locating the memory leak

_CrtSetBreakAlloc(<allocation number>)
• Sets a breakpoint on a specified object allocation order number
(debug version only).

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Memory Leaks
Locating the memory leak

Drill down through Call Stack window to find the last called function that
belongs to your application. (not a function from a library)

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


So what have we talked about. . .
• You will spend your time finding semantic errors
because the compiler catches syntax errors.
• Visual Studio has a rich suite of debugging tools to help
you Trace the execution and Watch variables.
• Control program execution by stopping and stepping
through your code.
• Watch variable values to see if they look right.
• Use the C Run-time library for finding memory leaks.

Microsoft Visual Studio is a registered trademark of Microsoft Corp.


Debugging in Visual Studio
Any questions?

You can contact me in the forums at the Game Institute or


you can email me at:
smjones@gameinstitute.com
info@gameengineer.net

Microsoft Visual Studio is a registered trademark of Microsoft Corp.

You might also like