Memory leakage check

Source: Internet
Author: User

There are many examples and Code on the Internet for checking memory leaks. The basic method is to use macros to replace the memory allocation and release functions. However, none of the many online examples are suitable for our company's needs.
The specific requirements for Memory leakage check are as follows:
1. The code for memory leak check should occupy as little CPU and memory as possible
2. Try not to affect the original program

This is because our server programs are leaked in special circumstances, which is hard to be simulated at ordinary times.
My previous practices for Memory leakage in this case are as follows:
1. Use the file Writing Method to record all memory allocation and release operations
2. Write another tool to analyze all the records and find the leaked code.
This requires a lot of hard disk space. However, it doesn't matter. The hard disk is cheap now!
 

However, you need to consider that the server program contains EXE and multiple DLL. For general purpose, the memory leakage check is divided into the following parts:
1. imemleak. h imemleak. cpp is added to each module.
2. memleaklog. dll records all memory operations in a unified manner and records them into files.
3. memchecktool.exe analysis tool

 

//IMemLeak.h#ifndef _YG_MEMDBG_H_#define _YG_MEMDBG_H_#include <cstdlib>//Redefines#define malloc(size) mallocb(size, __FILE__, __LINE__)#define free(memblock) freeb(memblock, __FILE__, __LINE__)#define realloc(memblock, size) reallocb(memblock, size, __FILE__, __LINE__)#define calloc(num, size) callocb(num, size, __FILE__, __LINE__)//Redefined functionsvoid * mallocb(size_t size, const char *pszFile, int nLine);void freeb(void *memblock, const char *pszFile, int nLine);void * reallocb(void *memblock, size_t size, const char *pszFile, int nLine);void * callocb(size_t num, size_t size, const char *pszFile, int nLine);//For C++void * operator new(size_t size, const char *pszFile, int nLine);void * operator new[](size_t size, const char *pszFile, int nLine);void operator delete(void *pvMem) throw();void operator delete[](void *pvMem) throw();void pre_delete(const char *pszFile, int nLine);//Redefine new and delete#define new     new(__FILE__, __LINE__)#define delete    pre_delete(__FILE__, __LINE__),delete#endif
//IMemLeak.cpp#include <stdio.h>#include <tchar.h>#include <stdlib.h>#include <malloc.h>#include <Windows.h>#include <cstdlib>enum EOperateType{    Type_Malloc,    Type_Calloc,    Type_Realloc,    Type_New,    Type_New_Array,    Type_Free,    Type_Delete,    Type_Delete_Array};typedef void (__stdcall * pFun_MemLeakLog)(LPCSTR PLog);pFun_MemLeakLog MemLeakLog = NULL;void CheckMemLeakLogDLL(){    if (MemLeakLog == NULL)    {        HINSTANCE  hinstLib = LoadLibrary(_T("MemLeakLog.dll"));        if (hinstLib != NULL)        {            MemLeakLog = (pFun_MemLeakLog)GetProcAddress(hinstLib, "MemLeakLog");        }    }}void Log(EOperateType type, void * pmem, size_t size, int nLine, const char* pszFile){    CheckMemLeakLogDLL();    char temp[1024];    if (MemLeakLog != NULL)    {        memset(temp, 0, 1024);        sprintf_s(temp, 1024, "%d-%p-%d-%d [%s]\n", type, pmem, size, nLine, pszFile);        MemLeakLog(temp);    }}void * mallocb(size_t size, const char *pszFile, int nLine){    void * pRet = malloc(size);    Log(Type_Malloc, pRet, size, nLine, pszFile);    return pRet;}void * callocb(size_t num, size_t size, const char *pszFile, int nLine){    void * pRet = calloc(num, size);    Log(Type_Calloc, pRet, size, nLine, pszFile);    return pRet;}void freeb(void *memblock, const char *pszFile, int nLine){    if (memblock)    {        Log(Type_Free, memblock, 0, 0, "NULL");    }    free(memblock);}void * reallocb(void *memblock, size_t size, const char *pszFile, int nLine){    void * pRet;    pRet = realloc(memblock, size);    Log(Type_Free,    memblock, size, nLine, pszFile);    Log(Type_Realloc, pRet, size, nLine, pszFile);    return pRet;}void * operator new(size_t size, const char *pszFile, int nLine){    void * pRet = malloc(size);    Log(Type_New, pRet, size, nLine, pszFile);    return pRet;}void * operator new[](size_t size, const char *pszFile, int nLine){    void * pRet = malloc(size);    Log(Type_New_Array, pRet, size, nLine, pszFile);    return pRet;}//#include <new>void operator delete(void *memblock) throw (){    if (memblock)    {        Log(Type_Delete, memblock, 0, 0, "NULL");    }    free(memblock);}void operator delete[](void *memblock) throw(){    if (memblock)    {        Log(Type_Delete_Array, memblock, 0, 0, "NULL");    }    free(memblock);}void pre_delete(const char *pszFile, int nLine){    }

 

 

Note:
A. I am writing the output directory to death.
B. Add the/Fc option in the project to be checked. Project-> properties-> Configuration-> C/C ++-> advanced-> use full path Yes (/FC)
C. Copy memleaklog. DLL to the directory of the process with the detected Memory leakage

Here is an example that you can see at a glance.

 

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.