Detect and Fix C++ Memory Leaks with AddressSanitizer

Salik Tariq
6 min readNov 4, 2023

--

Memory management is a critical aspect of programming in C++, where the programmer is responsible for dynamically allocating and deallocating memory. Improper handling of this memory can lead to issues such as memory leaks, where allocated memory is not freed even after it is no longer needed. Memory leaks can cause an application to consume more and more memory over time, leading to decreased performance and eventual failure. To combat this, developers use various tools and techniques. One such powerful tool provided by modern compilers is the AddressSanitizer (ASan).

This guide will cover the AddressSanitizer, focusing on its use in detecting and eradicating memory leaks in C++ applications. We’ll discuss what memory leaks are, how to use -fsanitize=address to detect them, and how to fix the issues highlighted by the tool.

Understanding Memory Leaks in C++

A memory leak occurs in a computer program when it allocates memory but fails to release it back to the system after its use is done. Over time, leaked memory accumulates and can exhaust the available memory, leading to various problems.

Examples of Memory Leaks in C++

Here are some typical scenarios that result in memory leaks in C++:

Not Deleting Dynamically Allocated Memory

int main() {
int *intPtr = new int(10);
// some code that doesn't delete the allocated memory
return 0; // intPtr goes out of scope here, and the allocated memory is never deleted
}

Failure to Delete Memory Upon Exception

void process() {
int *array = new int[100];
if (someConditionThatFails()) {
throw std::runtime_error("Error occurred!"); // Memory leak here if exception is thrown, as 'delete[]' is never reached
}
// some other code
delete[] array;
}

Memory Overwrite

int main() {
int *array = new int[10];
int *leak = new int(20);
array[10] = 0; // Out-of-bounds write, 'leak' pointer could be overwritten
delete[] array;
// Potential memory leak as 'leak' might have been lost due to overwrite
}

AddressSanitizer: A Brief Introduction

AddressSanitizer (ASan) is a fast memory error detector that can find various memory-related issues such as:

  • Out-of-bounds accesses
  • Use-after-free errors
  • Double-free errors
  • Memory leaks

It’s integrated into LLVM/Clang and GCC compilers and can be enabled by using the -fsanitize=address flag during the compilation and linking of the program.

Using -fsanitize=address to Detect Memory Leaks

Step 1: Compiling with AddressSanitizer

To use AddressSanitizer, you need to compile and link your C++ program with the -fsanitize=address flag. Here’s how you can do it with g++ or clang++:

g++ -fsanitize=address -g -O1 my_program.cpp -o my_program

Or with clang++:

clang++ -fsanitize=address -g -O1 my_program.cpp -o my_program

The -g flag includes debugging information in the binary, which is helpful for ASan to report errors with file names and line numbers. The -O1 is an optimization level that is compatible with ASan; higher levels might interfere with the sanitizer’s ability to detect issues.

Step 2: Running the Program

Run your program as usual. AddressSanitizer will monitor the execution and report any memory leaks or other memory issues it detects.

./my_program

Step 3: Interpreting the Output

If AddressSanitizer finds a memory leak, it will print a detailed report that includes the location where the leaked memory was allocated and where the program exited without freeing it. The report will look something like this:

=================================================================
==12345==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 400 byte(s) in 1 object(s) allocated from:
#0 0x7f6e5d2b2b50 in __interceptor_malloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cpp:144
#1 0x7f6e5d0c67cd in main /home/user/my_program.cpp:6
#2 0x7f6e5cd37b96 in __libc_start_main (/usr/lib/libc.so.6+0x21b96)
SUMMARY: AddressSanitizer: 400 byte(s) leaked in 1 allocation(s).

The report points out that 400 bytes were leaked, where the allocation happened, and the call stack at the point of allocation.

Step 4: Fixing the Leak

Once you have identified the leak, you need to ensure that every dynamically allocated memory is properly deallocated. In the above example, the allocation happened in my_program.cpp at line 6. You’d go to that line and see what’s missing — likely a delete or delete[] statement.

After fixing the leaks, recompile your program with the AddressSanitizer again and rerun it to ensure that the leaks are gone.

Real-World Example and Solution

Let’s consider a more realistic example of a memory leak and how we can detect and fix it using AddressSanitizer.

#include <iostream>
class DataHolder {
public:
DataHolder() {
mData = new int[10];
}
// A destructor should be defined to release the allocated memory
~DataHolder() {
delete[] mData;
}
void processData() {
// Processing the data
}
private:
int* mData;
};
int main() {
DataHolder* holder = new DataHolder();
holder->processData();
// We forgot to delete the 'holder' instance
// delete holder;
return 0; // Memory leak occurs here as 'holder' is not deleted
}

Compile this with AddressSanitizer:

g++ -fsanitize=address -g -O1 DataHolder.cpp -o DataHolder

Running the DataHolder program will generate a report from AddressSanitizer indicating that there is a leak at the point where we create an instance of DataHolder but never delete it.

After examining the report, you would add a delete holder; statement before the return in main() to ensure that the dynamically allocated DataHolder is properly deleted when it is no longer needed.

int main() {
DataHolder* holder = new DataHolder();
holder->processData();
delete holder; // This will properly deallocate the memory
return 0;
}

Recompile and rerun the program with AddressSanitizer. No leaks should be reported, indicating that the issue has been resolved.

Advanced Features and Considerations

Leak Detection at Program Exit

By default, AddressSanitizer reports memory leaks that are detected at the exit of the program. However, for long-running applications, you might want to check for leaks without stopping the program. ASan provides an interface for this through the __lsan_do_leak_check() function, which you can call at any point in your program to perform a leak check.

Dealing with False Positives

Sometimes AddressSanitizer may report what appears to be leaks but are actually intentional long-lived allocations or are managed by some custom memory manager. You can use the LSAN_OPTIONS environment variable to ignore certain leaks or to turn off leak detection altogether:

LSAN_OPTIONS=detect_leaks=0 ./my_program

Performance Overhead

AddressSanitizer introduces a runtime overhead — typically around 2x slower execution time and a larger memory footprint. This is important to consider during development and testing, as you may not want to use ASan in a production environment.

Environment Compatibility

AddressSanitizer works on most Unix-like systems and has support on Windows when using Clang or recent versions of GCC. Ensure compatibility with your development and deployment environments when planning to use ASan.

Conclusion

Detecting and fixing memory leaks is a vital part of developing robust C++ applications. Tools like AddressSanitizer provide a powerful means to find and eradicate these issues early in the development cycle. By compiling your code with -fsanitize=address and interpreting the detailed reports it provides, you can ensure that your C++ applications are free of memory leaks and other memory-related issues. Remember to use ASan as part of a broader testing strategy that includes unit tests, integration tests, and code reviews to maintain high-quality, leak-free code.

Below is a list of references that can provide further details on memory management, AddressSanitizer, and using sanitizers with C++:

AddressSanitizer Documentation for Clang/LLVM

  • The official documentation provides in-depth information on using AddressSanitizer with Clang, including various flags and environment variables. Clang AddressSanitizer Documentation

GCC Wiki on AddressSanitizer

  • The GCC Wiki offers guidance on using AddressSanitizer with GCC compilers, with examples and explanations of common issues. GCC Wiki — AddressSanitizer

LeakSanitizer Documentation

Valgrind Quick Start Guide

  • While not specific to AddressSanitizer, Valgrind is another tool for memory debugging. Its documentation can provide context on memory leak detection and may include comparative notes on different tools. Valgrind Documentation

Each of these resources can provide additional detail and breadth to the discussion of memory management and tooling in C++. They are useful for both novice and experienced C++ programmers seeking to deepen their understanding and improve their practices.

--

--

Salik Tariq
Salik Tariq

Written by Salik Tariq

Software engineer and technical author specialising in emerging trends.