Article by Ayman Alheraki on January 11 2026 10:35 AM
As C++ developers, ensuring that our programs are efficient, reliable, and free from memory issues is one of the biggest challenges we face. Bugs like memory leaks, invalid memory access, race conditions, and uninitialized memory usage can lead to serious problems, including crashes, undefined behavior, or performance degradation. Google Sanitizers offer a robust set of tools to help detect and debug these issues during development.
In this detailed guide, we will explore Google Sanitizers, their use cases, how to integrate them into your workflow, and compare them with other tools like Valgrind.
Google Sanitizers are lightweight, efficient tools integrated into modern compilers like GCC and Clang. They provide dynamic analysis for C++ programs, enabling developers to catch memory and threading errors early in the development process. The key tools in the suite include:
AddressSanitizer (ASan): Detects invalid memory accesses.
MemorySanitizer (MSan): Identifies uninitialized memory usage.
ThreadSanitizer (TSan): Finds race conditions in multi-threaded applications.
LeakSanitizer (LSan): Pinpoints memory leaks.
AddressSanitizer is one of the most widely used tools for detecting:
Buffer overflows.
Use-after-free errors.
Double frees and invalid frees.
Ensure you are using a compiler that supports ASan, such as GCC or Clang.
Compile your program with the
-fsanitize=addressflag:
xxxxxxxxxxg++ -fsanitize=address -g -o program program.cppRun the program as usual:
xxxxxxxxxx./programx
void bufferOverflow() { int arr[5]; arr[10] = 42; // Error: Out-of-bounds write}
int main() { bufferOverflow(); return 0;}When executed with ASan, the output will identify the issue:
xxxxxxxxxx===================================================================12345==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffd12345abcWRITE of size 4 at 0x7ffd12345abc thread T0 #0 0x4005ed in bufferOverflow (example.cpp:5) #1 0x400607 in main (example.cpp:9)=================================================================MemorySanitizer is designed to catch errors caused by using uninitialized memory, which can lead to unpredictable behavior.
Use Clang as your compiler.
Compile your program with the
xxxxxxxxxx-fsanitize=memoryflag:
xxxxxxxxxxclang++ -fsanitize=memory -g -o program program.cppRun the program:
xxxxxxxxxx./programxxxxxxxxxx
int main() { int uninitializedVar; std::cout << uninitializedVar << std::endl; // Error: Using uninitialized memory return 0;}MSan will report:
xxxxxxxxxx==12345==WARNING: MemorySanitizer: use-of-uninitialized-value #0 0x4005ed in main (example.cpp:5)ThreadSanitizer is invaluable for finding race conditions and other threading errors in multi-threaded programs.
Compile your program with
xxxxxxxxxx-fsanitize=thread:
xxxxxxxxxxg++ -fsanitize=thread -g -pthread -o program program.cppRun the program:
xxxxxxxxxx./programxxxxxxxxxx
int sharedVariable = 0;
void increment() { for (int i = 0; i < 1000; ++i) { ++sharedVariable; }}
int main() { std::thread t1(increment); std::thread t2(increment);
t1.join(); t2.join();
std::cout << "Shared Variable: " << sharedVariable << std::endl; return 0;}With TSan, you may see:
xxxxxxxxxx==================WARNING: ThreadSanitizer: data race Write of size 4 at 0x7ffd12345abc by thread T1 Write of size 4 at 0x7ffd12345abc by thread T2==================LeakSanitizer identifies memory allocations that are not freed, helping to eliminate memory leaks.
Compile your program with
xxxxxxxxxx-fsanitize=leak:
xxxxxxxxxxg++ -fsanitize=leak -g -o program program.cppRun the program:
xxxxxxxxxx./programxxxxxxxxxx
void memoryLeak() { int* ptr = (int*)malloc(10 * sizeof(int)); // Error: Memory not freed}
int main() { memoryLeak(); return 0;}LSan will output:
xxxxxxxxxx==12345==ERROR: LeakSanitizer: detected memory leaksDirect leak of 40 byte(s) in 1 object(s) allocated from: #0 0x4c2d1b2 in malloc (vg_replace_malloc.c:309) #1 0x4005ed in memoryLeak (example.cpp:4)You can use multiple sanitizers simultaneously to catch a wider range of issues:
xxxxxxxxxxg++ -fsanitize=address,leak -g -o program program.cppEnable Debug Information: Use -g for detailed stack traces.
Integrate with CI/CD Pipelines: Automate testing with sanitizers in your development workflow.
Suppression Files
: Ignore known false positives or irrelevant issues using suppression files.
xxxxxxxxxxASAN_OPTIONS=suppressions=leak.supp ./program| Feature | Google Sanitizers | Valgrind |
|---|---|---|
| Performance | Fast, minimal overhead | Slower, high overhead |
| Supported Platforms | Primarily Linux and macOS | Cross-platform |
| Detection Accuracy | High for runtime errors | High for all errors |
| Ease of Integration | Easy with modern compilers | Requires separate tools |
Google Sanitizers provide a robust, efficient solution for detecting memory and threading errors in C++ programs. By integrating these tools into your workflow, you can identify and fix potential issues early, resulting in more reliable and performant applications. Whether you are debugging memory leaks with LSan, addressing race conditions with TSan, or ensuring memory safety with ASan and MSan, these tools are invaluable for modern C++ development.