A Visual Studio Code extension designed to help C++ developers detect potential memory leaks through both static and runtime analysis.
- Static Analysis: Integrates
cppcheckto scan your code without executing it, identifying potential memory leaks and other common programming errors. - Runtime Analysis: Utilizes the powerful AddressSanitizer (ASan) from GCC/Clang to detect memory leaks accurately when you run or debug your program.
Before using this extension, please ensure you have the following tools installed and available in your system's PATH:
-
A C++ Compiler:
g++orclang++that supports AddressSanitizer. -
cppcheck: The static analysis tool.
# On Debian/Ubuntu sudo apt-get update && sudo apt-get install -y g++ cppcheck
This feature checks your currently open file for potential issues.
- Open any C or C++ file (
.c,.cpp). - Open the Command Palette (
Ctrl+Shift+PorCmd+Shift+Pon Mac). - Type and select
C++ Memory Check: Analyze Current File. - Any potential issues found by
cppcheckwill appear with squiggly lines in your code and will be listed in the Problems panel (Ctrl+Shift+M).
This feature configures your project for compilation and debugging with AddressSanitizer to find actual leaks at runtime.
Step A: One-Time Configuration
- Open the Command Palette (
Ctrl+Shift+P). - Type and select
C++ Memory Check: Configure for Runtime Analysis. - This will automatically create or update
tasks.jsonandlaunch.jsoninside your project's.vscodefolder.
Step B: Running the Analysis
- Open the C++ file you want to test (e.g.,
test.cpp). - Go to the Run and Debug view from the side bar.
- From the configuration dropdown at the top, select
C++: Run with AddressSanitizer. - Press the green play button (or
F5) to start debugging. - Once your program finishes, check the Debug Console panel for a detailed memory leak report from AddressSanitizer.
-
Static Analysis: The extension runs the
cppcheckcommand-line tool on the active file. It uses a custom output template to reliably parse the results and transform them into VS Code Diagnostics. -
Runtime Analysis: The extension automates the setup process by:
- Creating a build task in
tasks.jsonthat compiles your code usingg++with the-gand-fsanitize=addressflags. - Creating a launch configuration in
launch.jsonthat specifies the instrumented executable and links it to the build task.
- Creating a build task in
This project is licensed under the MIT License.