Memory leaks # # # mtrace # mtrace

linux provide mtrace / muntrace program to detect whether a memory leak. In general it is checked if a piece of code which has a memory leak, you can use this to wrap function. mtrace each implementation of malloc-free, malloc if each has a corresponding free, it means no memory leaks, memory leak problem for any non malloc / free what happened, mtrace and can not find out. That is, for the new memory, can only detect a leak, but can not be located. And therefore may not apply to C ++

Before using mtrace, first to set an environment variable "MALLOC_TRACE" to specify the file name mtrace generate test results. This file can be seen whether the code has memory leaks. MALLOC_TRACE export MALLOC_TRACE = xxx may be used to set, you may be provided by setenv.

setenv("MALLOC_TRACE","mtrace_test_output",1);

mtrace_test_output is the name of the file to store test results. But the format of the test results is the average person can not understand, but as long as the installation mtrace, then there will be one for the Perl script mtrace in shell enter the following commands: mtrace [binary] [file] will be output_file_name content is converted into energy be understood statement.

mtrace mtrace_test mtrace_test_outpute

 

mtrace / muntrace is a C function declarations and definitions in <mcheck.h>, the function prototype:

 void mtrace(void);
 void muntrace(void);

In fact mtrace is similar malloc_hook of malloc handler, the handler function just mtrace system has been written just for you.

Now we come to a series of tests mtrace work.

Memory allocation functions : malloc, calloc, realloc, _alloca , new, free, delete please refer to:

https://blog.csdn.net/xiaoting451292510/article/details/105094625

#include <iostream>
#include <mcheck.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
class new_delete_test
{
public:
	new_delete_test() :
		m_member(0x00)
	{
		printf("contructor\r\n");
	}
	~new_delete_test()	{
		printf("detructor\r\n");
	}
private:
	int m_member;
};
int main()
{
	setenv("MALLOC_TRACE","mtrace_test_output",1);
	mtrace();
	printf("malloc & new\r\n");
	void *p_malloc = malloc(100);
	void *p_malloc_no_free = malloc(100);
	void *p_calloc = calloc(10, 10);
	void *p_calloc_no_free = calloc(10, 10);
	void *p_realloc = malloc(100);
	p_realloc = realloc(p_realloc, 10);
	p_realloc = realloc(p_realloc, 50);
	p_realloc = realloc(p_realloc, 100);
	p_realloc = realloc(p_realloc, 200);
	void *p_realloc_no_free = malloc(1);
	p_realloc_no_free = realloc(p_realloc, 100);
	int *p_new = new int;
	int *p_new_no_delete = new int;
	int *p_new_array = new int[100];
	int *p_new_array_no_delete = new int[100];
	new_delete_test *p_new_class =  new new_delete_test;
	new_delete_test *p_new_class_no_free =  new new_delete_test;
	new_delete_test *p_new_class_array =  new new_delete_test[3];
	new_delete_test *p_new_class_array_no_free =  new new_delete_test[3];
	printf("***********************************************\r\n");

	printf("free & delete\r\n");
	free(p_malloc);
	p_malloc = NULL;
	free(p_calloc);
	p_calloc = NULL;
	free(p_realloc_no_free);
	p_realloc_no_free = NULL;
	delete p_new;
	p_new = NULL;
	delete p_new_array;
	p_new_array = NULL;
	delete p_new_class;
	p_new_class = NULL;
	delete[] p_new_class_array;
	p_new_class_array = NULL;
	printf("***********************************************\r\n");
	muntrace();

	return 0;
}


Compiled mtrace_test

g++ -Wall -g mtrace_test.cpp -o mtrace_test

Run mtrace_test

./mtrace_test

Generate mtrace_test_output file

Run the following command

mtrace mtrace_test mtrace_test_output 

You can get a memory leak information

Memory not freed:
-----------------
           Address     Size     Caller
0x000000000147d3b0    0x400  at 0x7f4ea91dc1d5
0x000000000147d830     0x64  at /home/cll/99_temp/memory_leak/mtrace/mtrace_test.cpp:26
0x000000000147d910     0x64  at /home/cll/99_temp/memory_leak/mtrace/mtrace_test.cpp:28
0x000000000147d980      0x1  at /home/cll/99_temp/memory_leak/mtrace/mtrace_test.cpp:34
0x000000000147dae0      0x4  at 0x7f4ea97f6c75
0x000000000147dca0    0x190  at 0x7f4ea97f6c75
0x000000000147de60      0x4  at 0x7f4ea97f6c75
0x000000000147dea0     0x14  at 0x7f4ea97f6c75

For any non-memory leak malloc / free what happened, mtrace and can not find out. That is, for the new memory, can only detect a leak, but can not be located.

 

Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/105094202