内存泄漏查找c++ new delete

#include <iostream>
#include <stdlib.h>

struct MemIns
{
    void *pMem;
    int m_nSize;
    char m_szFileName[256];
    int m_nLine;

    MemIns *pNext;
};

class MemManager
{
public:
        MemManager();
        ~MemManager();

private:
        MemIns *m_pMemInsHead;
        int m_nTotal;

public:
        static MemManager* GetInstance();
        void Append(MemIns *pMemIns);
        void Remove(void *ptr);
        void Dump(); 
};

void *operator new(size_t size, const char*szFile, int nLine);
void operator delete(void*ptr, const char*szFile, int nLine);
void operator delete(void*ptr);
void *operator new[] (size_t size, const char*szFile, int nLine);
void operator delete[](void*ptr, const char*szFile, int nLine);
void operator delete[](void *ptr);

#include"memchecker.h"
#include<stdio.h>
#include<malloc.h>
#include<string.h>

MemManager::MemManager()
{
    m_pMemInsHead=NULL;
    m_nTotal=NULL;
}

MemManager::~MemManager()
{

}

void MemManager::Append(MemIns *pMemIns)
{
    pMemIns->pNext=m_pMemInsHead;
    m_pMemInsHead = pMemIns;
    m_nTotal+= m_pMemInsHead->m_nSize;
}

void MemManager::Remove(void *ptr)
{
    MemIns * pCur = m_pMemInsHead;
    MemIns * pPrev = NULL;

    while(pCur)
    {
        if(pCur->pMem ==ptr)
        {
            if(pPrev)
            {
                pPrev->pNext =pCur->pNext;
            }
            else
            {
                m_pMemInsHead =pCur->pNext;
            }

            m_nTotal-=pCur->m_nSize;
            free(pCur);

            break;
        }

        pPrev = pCur;
        pCur = pCur->pNext;
    }
}

void MemManager::Dump()
{
    MemIns *pp = m_pMemInsHead;

    while(pp)
    {
        printf( "File is %s\n", pp->m_szFileName );
        printf( "Size is %d\n", pp->m_nSize );
        printf( "Line is %d\n", pp->m_nLine );

        pp = pp->pNext;
    }
}

void PutEntry(void *ptr,int size,const char*szFile, int nLine)
{
    MemIns * p = (MemIns *)(malloc(sizeof(MemIns)));
    if(p)
    {
        strcpy(p->m_szFileName,szFile);
        p->m_nLine = nLine;
        p->pMem = ptr;
        p->m_nSize = size;

        MemManager::GetInstance()->Append(p);
    }
}

void RemoveEntry(void *ptr)
{

    MemManager::GetInstance()->Remove(ptr);
}

void *operator new(size_t size,const char*szFile, int nLine)
{
    void *ptr = malloc(size);

    PutEntry(ptr,size,szFile,nLine);

    return ptr;
}

void operator delete(void *ptr)
{
    RemoveEntry(ptr);

    free(ptr);
}

void operator delete(void*ptr,const char *file, int line)
{
    RemoveEntry(ptr);

    free(ptr);
}

void*operator new[] (size_t size,const char* szFile,int nLine)
{
    void * ptr = malloc(size);

    PutEntry(ptr,size,szFile,nLine);

    return ptr;
}

void operator delete[](void *ptr)
{
    RemoveEntry(ptr);
    free(ptr);
}

void operator delete[](void*ptr,const char*szFile,int nLine)
{
    RemoveEntry(ptr);
    free(ptr);
}

#define new new(__FILE__,__LINE__)

MemManager m_memTracer;

MemManager*MemManager::GetInstance()
{
    return &m_memTracer;
} 

int main(void)
{
    int *plen =new int ;

    *plen=10;

    delete plen;

    char *pstr=new char[35];

    strcpy(pstr,"hello memory leak");

    m_memTracer.Dump();

    return 0;
}


运行结果

[root@test memcheck]# ./memcheck 
File is memchecker.cpp
Size is 35
Line is 148


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值