C++ implements memory leak detection - under windows and ubuntu

Method of memory leak detection in vs2019 (reference article): https://blog.csdn.net/qq_45662588/article/details/118388328
Function parameter description: https://blog.csdn.net/gongluck93/article/details/78676996

1. Memory leak detection of vs2019 under windows

You don’t need to read the information below, it’s just to help me understand it easier.

  1. Map heap information to debug build: #define CRTDBG_MAP_ALLOC
  2. Add header file information:stdlib.h、crtdbg.h
  3. Implementing the memory detection method, although it is said to be implemented, is actually fixed. EnableMemLeakCheck calls the system's _CrtSetDbgFlag method:_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF)
    • With this bit set (on), the application can instruct the debug heap manager to perform special debugging operations, including checking for memory leaks when the application exits and reporting if any memory leaks are found, by specifying that freed memory blocks should remain in the heap Linked lists to simulate out-of-memory conditions;
    • _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF)
    • The parameter is the flag bit of the new state, and what is returned is the flag bit of the previous state;
    • _CRTDBG_LEAK_CHECK_DF: Indicates that an error report will be generated if the application fails to release all the memory it allocated.
    • _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) to get the current _crtDbgFlag status
  4. Mark the place called at the end of the memory leak: _CrtDumpMemoryLeaks();
bit field default describe
_CRTDBG_ALLOC_MEM_DF Open On: Enables debugging heap allocations and using memory block type identifiers, _CLIENT_BLOCKe.g. Off: Add the new allocation to the heap's linked list, but set the blocking type to **_IGNORE_BLOCK**. Can also be combined with any heap frequency checking macro.
_CRTDBG_CHECK_ALWAYS_DF closure On: Call _CrtCheckMemory on every allocation and deallocation request. Close: Must be called explicitly _CrtCheckMemory. When this flag is set, the heap frequency checking macro has no effect.
_CRTDBG_CHECK_CRT_DF closure _CRT_BLOCKOn: Includes types in leak detection and memory state difference operations . Off: These operations ignore memory used internally by the runtime library. Can also be combined with any heap frequency checking macro.
_CRTDBG_DELAY_FREE_MEM_DF closure Open: Place freed memory blocks in a heap linked list, allocate them of type **_FREE_BLOCK**, and then populate them with byte value 0xDD. Off: Do not keep freed blocks in the heap linked list. Can also be combined with any heap frequency checking macro.
_CRTDBG_LEAK_CHECK_DF closure Open︰Performs automatic leak checking on program exit by calling _CrtDumpMemoryLeaks , which generates an error report if the application fails to free all memory it allocated. Off: Do not automatically perform leak checks on program exit. Can also be combined with any heap frequency checking macro

Function name mnemonic :
CRT is a standard C function, Alloc means returning a pointer to n consecutive character storage units,
Dump means garbage

1. First detect which piece of memory is leaked

increased

#define CRTDBG_MAP_ALLOC  
#include <stdlib.h>  
#include <crtdbg.h>  
//在入口函数中包含 _CrtDumpMemoryLeaks();  
//即可检测到内存泄露

//定义函数:
inline void EnableMemLeakCheck()
{
    
    
	_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}
//该函数可以放在主函数的任意位置,都能正确的触发内存泄露输出


//以如下测试函数为例:
int main()
{
    
    
	EnableMemLeakCheck();
	char* pChars = new char[10];
	_CrtDumpMemoryLeaks();
	return 0;
}

As shown in the figure, it can be seen that the memory requested 191 times has not been released, resulting in a memory leak.
Insert image description here

2. Track exceptions

Depending on the previous memory requests, we can make the following settings:

  1. Same mapping flag;
  2. The same header file contains
  3. Replace the memory leak detection function EnableMemLeakCheck with _CrtSetBreakAlloc (number of memory applications);
#define CRTDBG_MAP_ALLOC  
#include <stdlib.h>  
#include <crtdbg.h>  

int main()
{
    
    
	_CrtSetBreakAlloc(191);
	char* pChars = new char[10];
	_CrtDumpMemoryLeaks();
	return 0;
}

After running, as shown in the figure, the debugging interface where the memory leak was applied for will be brought up. In fact, the stack information at that time is saved.
Insert image description here

2. Use clion to check memory leaks under Linux

In Linux, there is a mature and free product valgrind, which is specially used to detect memory leaks and can directly track the relevant locations. This tool is also integrated in clion. For advertising, vs2022 is recommended for C++ development under Windows, while under Linux I recommend clion, and clion can be used as a remote server. If you have a local server, clion is also recommended.

1. Environmental use

Install under ubuntu20 (there is no problem with other versions)

2. Installation

sudo apt install valgrind

3. Set up in clion

Go to settings to set up, setting–>build–>dynamic analysis–>valgrind

  • However, it will be automatically set in Ubuntu, and it will be the same as the default if installed through apt;
    Insert image description here

4. Run

Through valgrind instead of the usual one, you can complete the memory leak detection analysis by following the 1/2 step

  1. run
  2. View Results
    Insert image description here

Guess you like

Origin blog.csdn.net/weixin_42295969/article/details/127332865