Debugging in Visual Studio
Visual Studio provides powerful debugging capabilities for both C++ and C# code in Calculator.Starting a Debug Session
Set Your Startup Project
Right-click the Calculator project in Solution Explorer and select Set as Startup Project.
Choose Debug Configuration
Select Debug from the configuration dropdown in the toolbar. Debug builds include:
- Full debugging symbols
- Minimal optimization
- Enabled assertions
- Better stack traces
Breakpoints
Set breakpoints to pause execution and inspect state:Basic Breakpoints
- Click in the left margin next to a line of code
- Or press F9 with the cursor on a line
- Breakpoint appears as a red dot
Conditional Breakpoints
Right-click a breakpoint and select Conditions to:
- Break only when a condition is true
- Break when a value changes
- Break after a hit count threshold
Inspecting Variables
Visual Studio provides multiple ways to examine variables:| Tool | Shortcut | Description |
|---|---|---|
| Autos | Ctrl+Alt+V, A | Shows variables in current and previous statements |
| Locals | Ctrl+Alt+V, L | Displays all local variables in current scope |
| Watch | Ctrl+Alt+W, 1-4 | Custom watch windows for specific expressions |
| Quick Watch | Shift+F9 | Popup window for evaluating expressions |
| Data Tips | Hover | Hover over variables to see their values |
Pin frequently used variables in data tips to keep them visible as you step through code.
Stepping Through Code
Debugging Mixed C++/C# Code
Calculator uses both C++ (CalcManager, CalcViewModel) and C# (UI layer), requiring mixed-mode debugging:Enable Mixed-Mode Debugging
- Right-click the Calculator project
- Select Properties
- Go to Debug tab
- Check Enable native code debugging
Set Breakpoints in Both Languages
- Set breakpoints in C# UI code (Calculator project)
- Set breakpoints in C++ code (CalcManager, CalcViewModel)
- Debugger will break in both when hit
Debugging Specific Components
CalcManager (Calculation Engine)
The C++ calculation engine is in the CalcManager project:CalcManager uses arbitrary-precision arithmetic. Watch the internal number representation in the Number class for precision-related issues.
CalcViewModel (View Models)
The C++ view models bridge the UI and calculation engine:UI Layer (C# Code)
The XAML UI and C# code-behind:Advanced Debugging Techniques
Memory Analysis
For investigating memory leaks or high memory usage:Take Memory Snapshots
- Debug > Performance Profiler
- Select .NET Object Allocation Tracking and Memory Usage
- Start debugging
- Take snapshots at different points
- Compare snapshots to find leaks
Performance Profiling
Profile Calculator to identify performance bottlenecks:CPU Profiling
- Debug > Performance Profiler
- Select CPU Usage
- Click Start
- Perform operations in Calculator
- Stop profiling and analyze results
XAML Debugging
Debug UI issues with XAML-specific tools:Live Visual Tree
Debug > Windows > Live Visual Tree to:
- Inspect the visual element hierarchy
- Find elements by type or name
- Navigate to XAML source
Live Property Explorer
Debug > Windows > Live Property Explorer to:
- View and modify properties in real-time
- See style and template sources
- Debug data binding issues
Diagnostic Data
Calculator includes diagnostic telemetry that can be enabled for debugging:Diagnostic data is disabled by default in development builds and requires explicit opt-in via the build flag.
Logging and Tracing
Calculator uses the TraceLogging project for diagnostic output:- Windows Performance Recorder (WPR)
- Windows Performance Analyzer (WPA)
- PerfView
Common Debugging Scenarios
Calculation Errors
Inspect Command Flow
Watch:
commandparameter value- Current state in
m_currentState - Display value in
m_displayValue
UI Not Updating
Check Data Binding
In Live Property Explorer, verify:
- Binding source is correct
- Property change notifications fire
- Values are being updated
Crashes on Startup
Enable First-Chance Exceptions
Debug > Windows > Exception SettingsCheck:
- C++ Exceptions
- Common Language Runtime Exceptions
Debugging Tests
Unit Tests
Debug failing unit tests:UI Tests
Debug WinAppDriver UI tests:Attach to Both Processes
Attach debugger to:
- CalculatorUITests.exe (test process)
- Calculator.exe (app under test)
Debugging Tools
Built into Visual Studio
- Diagnostic Tools - Real-time CPU, memory, and events
- Performance Profiler - Deep performance analysis
- Live Visual Tree - XAML element inspection
- IntelliTrace - Historical debugging (Enterprise only)
External Tools
- WinDbg - Advanced native debugging
- PerfView - Performance and memory analysis
- Sysinternals Suite - Process Explorer, Process Monitor
- ETW/WPR/WPA - Event tracing and analysis
Tips and Best Practices
Next Steps
Contributing Guidelines
Learn how to contribute fixes and features
Architecture
Understand Calculator’s architecture