Home > Article > Backend Development > How to find memory leaks in C++ using Valgrind or AddressSanitizer?
To find memory leaks in C, you can take advantage of Valgrind and AddressSanitizer. Valgrind dynamically detects leaks, showing address, size and call stack. AddressSanitizer is a Clang compiler plugin that detects memory errors and leaks. To enable ASan leak checking, use the --leak-check=full option when compiling, which will report leaks after the program is run.
How to use Valgrind or AddressSanitizer to find memory leaks in C
Introduction
Memory leaks is a common problem in languages like C. To detect and resolve these leaks, tools like Valgrind and AddressSanitizer can be used.
Use Valgrind to find memory leaks
Valgrind is a dynamic memory debugging tool that can detect memory leaks. To use Valgrind:
valgrind ./my_program
Valgrind will run the program and report any detected memory leaks. The output will show the leaked address, size, and call stack.
Example
The following C code example demonstrates how Valgrind detects memory leaks:
int* ptr = new int[10]; // ... // 忘记释放 ptr
Running this code and using Valgrind will output the following results:
==8445== LeakSanitizer: detected memory leaks ==8445== Direct leak of 40 bytes in 1 object(s) allocated from: #0 0x49f2c0 in default_new_allocator_000000157013e0000000 ::operator() () (_libunwind.dylib:0x103d8e000) #1 0x41626f in create_array () in /tmp/a.out:10 #2 0x415b2d in main () in /tmp/a.out:15 SUMMARY: ==8445== LEAK SUMMARY: ==8445== definitely lost: 40 bytes in 1 object(s)
The output shows that 40 bytes were leaked and allocated at address 0x49f2c0.
Finding memory leaks using AddressSanitizer
AddressSanitizer (ASan) is a Clang compiler plugin that can detect memory errors, including memory leaks. To use ASan:
clang++ -fsanitize=address ...
ASan will detect memory access errors and generate a crash report when an error occurs. To check for memory leaks, run the program twice:
./my_program # 第一次运行 ./my_program --leak-check=full # 第二次运行,启用泄漏检查
The second run will report any detected memory leaks.
Example
The following C code example demonstrates how AddressSanitizer detects memory leaks:
int* ptr = new int[10]; // ... // 忘记释放 ptr
Compiling and running this code, with ASan enabled, will output the following results:
==28847== ERROR: AddressSanitizer: detected memory leaks SUMMARY: ==28847== LeakSanitizer: 40 byte(s) leaked in 1 allocation(s). ==28847== 0x7fdd1b000010 40 bytes in 1 block ==28847== LeakSanitizer: ==28847== Direct leak of 40 bytes in 1 object(s) allocated from: ==28847== #0 0x7fdd17a346c0 in __sanitizer::Allocator<std::__detail::__shared_count>::allocate(unsigned long) (_sanitizer.h:1195) ==28847== #1 0x7fdd184d0f90 in void* std::__detail::__shared_count<unsigned int>::allocate() (_shared_count.h:128) ==28847== #2 0x7fdd182de485 in void* std::__shared_ptr<int>::__clone() (_shared_ptr.h:256) ==28847== #3 0x48b935 in create_array() (/tmp/a.out:10) ==28847== #4 0x48b884 in main (/tmp/a.out:15)
The output shows that 40 bytes were leaked and allocated at address 0x7fdd1b000010.
The above is the detailed content of How to find memory leaks in C++ using Valgrind or AddressSanitizer?. For more information, please follow other related articles on the PHP Chinese website!