C + + program memory leak Detection Tool

Source: Internet
Author: User

  features :
Used to detect memory leaks in C + + programs.
Principle:
In fact very easy, is through the function of the overloaded mechanism, capturing the application of new, new[], delete, delete[], malloc,calloc,free and other memory manipulation functions.  
Features:
Because the user program memory allocation information needs to be recorded during the test, the tool itself must be dynamically allocated in memory. In order to improve the efficiency of memory allocation, the program implemented two linked lists.  
1, spare linked list, in fact, is a simple memory pool
Define a structure to save memory allocation information
typedef struct _TAGMEMORYINFO
{
??? void* addr;????????? Save the allocated memory address
??? size_t size;???????? Memory Size
??? _ul linenum;????? Call the line number of the memory allocation function
??? Char Filename[max_file_len];? File Name
}memoryinfo;

The list structure of the memory allocation information, which is defined as the union type, in order to save the next member's overhead
Union FreeList
{
??? Freelist* Next; 
??? Memoryinfo data; 
};
2. The linked list of memory information is currently being saved 
typedef struct _TAGBUSYLIST
{
??? _tagbusylist* Next; 
??? memoryinfo* data; 
}busylist;
Insufficient: 
1, only on the vc2005TestPassed, not on other platformsTest
2. Multithreading is not supported (perhaps supported) 
3, save the current memory allocation information linked list, there is a memory cost of the next field. 
Source: 
1. header file 
#ifdef Detect_memory_leak
#ifndef _detect_memory_leak_h_
#define _detect_memory_leak_h_
typedef unsigned long _ul;
void* __cdecl operator new (unsigned int size, _ul linenum, const char* file);
void* __cdecl operator new[] (unsigned int size, _ul linenum, const char* file);
void __cdecl operator delete (void *p);
void __cdecl operator delete [] (void *p);
void __cdecl operator delete (void *p,? _ul linenum, const char* file);
void __cdecl operator delete [] (void *p,? _ul linenum, const char* file);
void* __cdecl _debugmalloc (size_t size, _ul linenum, const char* file);
void* __cdecl _debugcalloc (size_t num, size_t size, _ul linenum, const char* file);
void? __cdecl _debugfree (void* addr);
#ifndef Detect_memory_leak_impl
#define NEW Debug_new
#define DEBUG_NEW NEW (__line__, __file__)
#define MALLOC Debug_malloc
#define DEBUG_MALLOC (x) _debugmalloc (x, __line__, __file__)
#define CALLOC Debug_calloc
#define DEBUG_CALLOC (x) _debugcalloc (x, __line__, __file__)
#define FREE Debug_free
#define DEBUG_FREE (x) _debugfree (x)
#endif
void Dumpleakedmemoryinfo ();
#endif//_detect_memory_leak_h_
#endif//detect_memory_leak

2. Source Files
#define Max_file_len 128
Need to achievefunction
1 Write the allocated memory information to the file
2 Write the freed memory information to the file
3 Saves the allocated memory information in memory and provides an interface to report the current memory leak.
Define a structure to save memory allocation information
typedef struct _TAGMEMORYINFO
{
??? void* addr;????????? Save the allocated memory address
??? size_t size;???????? Memory size
??? _ul linenum;????? Call the line number of the memory allocation function
??? Char Filename[max_file_len];? Filename
}memoryinfo;
The list structure of the memory allocation information, which is defined as the union type, in order to save the next member's overhead
Union FreeList
{
??? Freelist* Next;
??? Memoryinfo data;
};
typedef struct _TAGBUSYLIST
{
??? _tagbusylist* Next;
??? memoryinfo* data;
}busylist;
Spare initial length of the linked list
#define Free_list_init_len 16
Spare the head pointer of a linked list
static freelist* g_freelist = NULL;
The head pointer of the linked list is being used
static busylist* g_busylist = NULL;
Declaration of internal Use functions
static void _createfreelist (int initlen);
static void _releasefreelist ();
Static void* _getfreenode ();
Static void* _getbusynode ();
static void _freenode (void* p);
static void _writememoryinfo (const memoryinfo* pInfo, bool balloc);
static void _storememoryallocinfo (void* addr, size_t size, _ul linenum, const char* file);
static void _storememorydeallocinfo (void* addr);
void* __cdecl operator new (unsigned int size, _ul linenum, const char* file)
{
??? void* p =:: operator new (size);
??? _storememoryallocinfo (p, size, linenum, file);
??? return p;
??? return 0;
}
void __cdecl operator Delete (void* p)
{
? _storememorydeallocinfo (P);
}
void __cdecl operator delete (void *p,? _ul linenum, const char* file)
{
??? LineNum;
? File
??? _storememorydeallocinfo (P);
}
void* __cdecl operator new[] (unsigned int size, _ul linenum, const char* file)
{
??? void* p =:: operator new (size);
??? 
??? _storememoryallocinfo (p, size, linenum, file);
??? 
??? return p;???
}
void __cdecl operator delete [] (void *p)
{
??? _storememorydeallocinfo (P);
}
void __cdecl operator delete [] (void *p,? _ul linenum, const char* file)
{
? LineNum;
? File
? _storememorydeallocinfo (P);
}
void* __cdecl _debugmalloc (size_t size, _ul linenum, const char* file)
{
??? void* p = malloc (size);
? _storememoryallocinfo (p, size, linenum, file);
? return p;
}
void* __cdecl _debugcalloc (size_t num, size_t size, _ul linenum, const char* file)
{
? void* p = calloc (num, size);
? _storememoryallocinfo (p, num * size, linenum, file);
? return p;
}
void? __cdecl _debugfree (void* addr)
{?
? _storememorydeallocinfo (addr);?
}
Create a list of spare nodes and generate a memory pool to record memory allocation information.
When memory is allocated frequently, it is not detectedToolsof its ownPerformance, affecting the application'sPerformance
void _createfreelist (int initlen)
{
??? freelist* p = (freelist*) malloc (sizeof (FreeList) * Initlen);
??? G_freelist = p;
??? for (int idx = 1; idx < Initlen; ++idx)
??? {
??????? P->next = p + idx;
??????? p++;
??? }
??? P->next = NULL;
}
void* _getfreenode ()
{
??? if (g_freelist = = NULL)
??? {
??????? _createfreelist (Free_list_init_len);
??????? if (NULL = = g_freelist)
??????? {
??????????? return NULL;
??????? }
??? }
??? 
??? freelist* p = g_freelist;???
??? G_freelist = g_freelist->next;
??? Return (void*) p;
}

void* _getbusynode (void* addr)
{
? if (g_busylist = = NULL)
? {
?? return NULL;
? }
? if (NULL = = G_busylist->next)
? {
?? memoryinfo* Retnode = NULL;
?? if (g_busylist->data->addr = = addr)
?? {
??? Retnode = g_busylist->data;
??????????? Delete g_busylist;
??? G_busylist = NULL;???????????
?? }

?? Return (void*) Retnode;
? }
? busylist* Pre, *curr;
? Pre = Curr = G_busylist;
? while (Curr)
? {
?? if (curr->data->addr = = addr)
?? {
??? busylist* tmp = Curr;
??? memoryinfo* Retnode = curr->data;
??? Pre->next = curr->next;
??? Free ((void*) TMP);
??? Return (void*) Retnode;
?? }
?? Pre = Curr;
?? Curr = curr->next;
? }
??? return NULL;
}
void _freenode (void* p)
{
??? if (NULL = = p)
??? {
??????? Return
??? }
??? freelist* Tmpnode = (freelist*) p;
??? Tmpnode->next = g_freelist;
??? G_freelist = Tmpnode;
}
Saving memory allocation information
void _storememoryallocinfo (void* addr, size_t size, _ul linenum, const char* file)
{
??? memoryinfo* node = (memoryinfo*) _getfreenode ();
??? if (NULL = = node)
??? {
??????? Return
??? }
??? Node->addr =addr;
??? node->size = size;
??? Node->linenum = LineNum;
?? 
??? size_t len = strlen (file);
??? Len = Len >= max_file_len? Max_file_len-1: LEN;
??? strncpy (node->filename, file, Len);
? Node->filename[len] = '/0 ';
??? Add linked List
??? busylist* Busynode = (busylist*) malloc (sizeof (busylist));
??? busynode->data = node;
??? if (g_busylist = = NULL)
??? {
??????? G_busylist = Busynode;
??????? Busynode->next = NULL;
??? }
??? Else
??? {
??????? Busynode->next = g_busylist;
??????? G_busylist = Busynode;
??? }
??? Write file
??? _writememoryinfo (node, true);
}
Saving memory allocation information
void _storememorydeallocinfo (void* addr)
{
? memoryinfo* node = (memoryinfo*) _getbusynode (addr);
? if (NULL = = node)
? {
?? Return
? }
? Write file
? _writememoryinfo (node, false);
??? _freenode ((void*) node);?
}
Write log function
void _writememoryinfo (const memoryinfo* pInfo, BOOL Balloc)
{
??? if (pInfo! = NULL)
??? {
??????? FILE *FP = fopen ("Debugmemorylog.txt", "A +");
??????? if (!FP)
??????????? Return
??????? fprintf (FP, "%p:/t%s/t%d line%s%d bytes/n", Pinfo->addr, Pinfo->filename, Pinfo->linenum/
??????????? , (Balloc?) "Allocated": "Freed"), pinfo->size);
??????? Fflush (FP);
??????? Fclose (FP);
??? }
}
Writes leaked memory information to disk
void Dumpleakedmemoryinfo ()
{
? FILE *FP = fopen ("Memoryleak.txt", "A +");
? if (!FP)
?? Return

? busylist* p = g_busylist;
? while (p)
? {
?? busylist* tmp = p;
?? memoryinfo* PInfo = tmp->data;
?? if (pInfo! = NULL)
?? {???
??? fprintf (FP, "%p:/t%s/t%d line leak%d bytes/n", Pinfo->addr, Pinfo->filename, Pinfo->linenum, pinfo->size);???
?? }
?? _freenode ((void*) pInfo);
?????? Delete tmp;
??? TMP = NULL;
??? p = p->next;
? }
? Fflush (FP);
? Fclose (FP);
? Frees the memory pool resource to the operating system
??? _releasefreelist ();
}
void _releasefreelist ()
{
??? while (g_freelist)
? {
?? freelist* tmp = g_freelist->next;
?? Delete g_freelist;
?? G_freelist = tmp;??
? }
}
#endif//detect_memory_leak

This article was selected from: http://www.spasvo.com/news/html/20141030112355.html

?

?

C + + program memory leak Detection Tool

Related Article

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.