Code Focused

C++ Debugging in Visual Studio: Let's Start with the Basics

Debugging is a crucial part of the development process. In this new series of tried and true C++ tips, we look at the basics of debugging and step through some more advanced debuggings tips for C++ developers.

We all know that debugging is an important part of software development process. Some might argue that it's best not to touch the debugger at all (and just write clean C++ code). The reality is that sooner or later you will encounter a situation where the debugger will save your code and/or career!

With the Visual Studio IDE you get one of the best-integrated debuggers for C++. For me, it's crucial to know how to use that tool efficiently. That's the aim here, and so I'd like to share a few tips and encourage you to search for even more advanced ways to use the debugger.

In the series, we'll cover:

  • The basics of the Visual Studio IDE.
  • How to change the version of the code at runtime (that way, you can test two or more solutions without recompiling).
  • How to improve the logger so that you can quickly move to the message source.
  • Conditional breakpoints
  • How to control to which function the debugger can step into

And that's just for starters. We'll also look at how to get more information about objects while debugging, how to investigate lots of objects, mouse events, and debugging visualizers.

This time out, let's start fresh and look at the basics of the VS IDE. Things you should do to get familiar with the IDE:

  • Learn the keyboard shortcuts! You should at least know the essential tasks like running the debugger, stepping in/out/over, continuing execution.
  • Manage breakpoints: Do you know how to set breakpoints, or how to enable or disable breakpoints? Did you know you can even export breakpoints?
  • Visual Studio 2015 offers a number of features for debugging. so why not use the latest version? Even if you still compile using VS 2013, you can open projects in VS2015, change the Platform Toolset to the version you need, and that way you're using the better debugger. Moreover, If you want to track performance or memory problems, it has a dedicated set of profiling tools.
  • Start a debugging session (via the F5 key) and just wait until debugger stops on your breakpoints.
  • Run the debugger to the cursor or pause the debugger to see what code is being executed at the moment. VS offers a way to break on various exceptions. There is a separate window (also improved in VS 2015) that you can control which exceptions will be handled.
  • When you hit a breakpoint you can observe the state of your application in various VS windows. Most notable are:
    • Call Stack windows shows where you are in the code, what chain of methods was called up to the breakpoint.
    • Autos and Locals window show current variables used in the app. What's more, Autos Window will also report the return value of the executed function.
    • Threads Window shows the currently active Threads. You can also use Parallel Stacks Window.aspx to get an even better visualization of the state of threads.
    • Output Window is one of the most useful windows: you can see your log messages here. All messages from OutputDebugString go there.
  • From a breakpoint, you'll usually move line by line to see the code execution. You can step into a function or step over. There is also a method to set the next statement -- that way you can skip some part of the code or repeat the steps.

Figure 1 shows shows a basic debugger session.

Basic Visual Studio Debugger
[Click on image for larger view.] Figure 1: Basic Visual Studio Debugger

This short article was just a quick run through some of the basic tools in the VS debugger, so you probably skipped through most of it because you know all of it already.

Stay tuned and next time we'll tackle one technique I am especially keen on: how to improve logging to the output window so that you can quickly jump to the source code.

About the Author

Bartlomiej Filipek is a software developer in Poland who specializes in C++, Windows and graphics programming. He worked for a number of companies developing multimedia, document editors, games, graphics drivers and flight planning systems, and has taught game development at local university. Follow Bart's blog at http://www.bfilipek.com and on Twitter @fenbf.

comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube