A method for detecting memory leaks by C + +

Source: Internet
Author: User

Today, while listening to a colleague doing a positive reply, I talked about how to detect memory leaks under the VxWorks code, because the company's products are prone to internal leaks, and positioning is very difficult. This reminds me of a problem I had encountered in ZW before, when I had studied how C language detects the internal leakage problem. Just on the internet has been a master has given a solution, so here to reprint a technical document, as their own technology accumulation of intravenous drip, I saw the approximate idea, then in 30 minutes to achieve a simple C language version. In the evening when HP came back, just complaining about their project because of the internal leakage and can not be tested, want to use my writing this thing to try, hope this thing can really solve the problem, for their own first praise, refueling. The following is a reprint address and full text: http://hi.baidu.com/%B6%A1%BC%D2%C0%CF%C8%FD/blog/item/059b3a8aae2f6ddbfd1f10f8.html

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------

Memory leaks are a common problem in C programming, and because of the slow-moving problems caused by memory leaks, it is not easy to perceive, so it is necessary to write a simple program to detect memory leaks.

Memory leaks usually refers to the heap memory leakage, that is, through the malloc, Calloc function application of memory, so the memory leak detection method The core idea is to define custom memory allocation and release functions through macros to replace the user's malloc, calloc, free and other functions. The design data structure records the memory request information, when requests the memory, records and inserts into the global list, when releases the memory, looks for the corresponding record from the global linked list, and deletes. At the end of the program, write the information in the linked list to the file and clear the list.

Replace the malloc, calloc, free functions with a macro definition and insert the __file__, __line__ position statement as shown below.

#define MALLOC (size) malloc_detector (size,__file__,__line__)
#define CALLOC (element_num,element_size) calloc_detector (element_num,element_size,__file__,__line__)
#define FREE (addr) free_detector (addr)

The design memory request information is as follows:

typedef struct __info{
void* addr; The memory address
unsigned char file[128]; The file of the memory Alloc statement
unsigned int line; The line of the memory Alloc statment
unsigned int size; The size of the memory alloced
}alloc_info;

typedef struct __node{
Alloc_info Info;
Alloc_info*next;
}alloc_info_node,*alloc_info_list;

The final information is written to the file output_file to open the file in "w+" mode

#define OUTPUT_FILE "Leak_detector_report.txt"

Before you define a function such as a substituted malloc, you must cancel the macro definition such as malloc through the UNDEF statement to prevent the problem of a dead loop before you can define a replacement function

#undef malloc
#undef calloc
#undef FREE

Malloc_detector, free_detector are defined as shown in the following code.

void *malloc_detector (size_t size,unsigned char *file,unsigned int line)
{
void *ptr=malloc (size);
Alloc_info Info;
Info.addr=ptr;
Info.line=line;
Info.size=size;
strncpy (info.file,file,127);
Add_info (info); ADD info to the global alloc_info_list head
return ptr;
}

void Free_detector (void *addr)
{
Delete_info (addr); Find and Delete the info which has the address==addr from Alloc_info_list head
Free (addr);
/*we only detecte The memory-leak is not the wrong free-addr which
}

Call the Report_info function at the end of the program to save the detection information.

void Report_info ()
{
FILE *fp_write=fopen (output_file, "w+");
if (!fp_write) {printf ("can" T open file\n); exit (1);}
Char info[sizeof (alloc_info) +128];
Alloc_info_list I=head,pre;
if (i==null) {
sprintf (Info, "%s", "No Memory Leak");
Fwrite (Info,strlen (info) +1,1,fp_write);
}
for (i=head;i!=null;)
{
sprintf (Info, "Memory leak:file%s line%d,addr%x,size%d\n",i->info.file,i->info.line,i->info.addr,i-> Info.size);
Fwrite (Info,strlen (info) +1,1,fp_write);
Pre=i;
i=i->next;
Free (pre);
}
Flcose (Fp_write);
}

The following two functions are linked list operations
void Add_info (alloc_info info); Add Memory alloc info to Alloc_info_list
void Delete_info (void *addr); Find and delete the Alloc info

The test procedure is as follows:

#define DEBUG
#include <stdio.h>
#include <malloc.h>
#include "leak_detector_c.h"
extern alloc_info_list head;
int main ()
{
Char *ptr1=malloc (100);
Char *ptr2=malloc (101);
Char *ptr3=malloc (102);

Char *ptr4=calloc (103,1);
Char *ptr5=calloc (104,1);
Char *ptr6=calloc (105,1);
Free (PTR1);
Free (PTR6);
Report_info ();
}

The test results are as follows:

Memory Leak:file f:\ personal Source library \ Memory leak processing method \leak_detctor_c_v2\text.c line 9,ADDR 32620,size 101
Memory Leak:file f:\ personal Source library \ Memory leak processing method \leak_detctor_c_v2\text.c line 10,addr 32728,size 102
Memory Leak:file f:\ personal Source library \ Memory leak processing method \leak_detctor_c_v2\text.c line 12,addr 32830,size 103
Memory Leak:file f:\ personal Source library \ Memory leak processing method \leak_detctor_c_v2\text.c line 13,addr 33ad0,size 104

Detect memory leaks, the default of each free address has been applied, that is, has been saved in the global list, you can expand the deletion function, if the entire list did not find that the corresponding alloc_info can be judged to be free error, for the location of free error is helpful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.