(9)简单判断某个独立函数有没有内存泄露可以用下面的方法:

class DbgMemLeak
{
    _CrtMemState m_checkpoint;

public:
    explicit DbgMemLeak()
    { 
        _CrtMemCheckpoint(&m_checkpoint);
    };

    ~DbgMemLeak()
    {
        _CrtMemState checkpoint;
        _CrtMemCheckpoint(&checkpoint);
        _CrtMemState diff;
        _CrtMemDifference(&diff, &m_checkpoint, &checkpoint);
        _CrtMemDumpStatistics(&diff);
        _CrtMemDumpAllObjectsSince(&diff);
    };
};


int _tmain(int argc, _TCHAR* argv[])
{
    DbgMemLeak check;
    {
        char* p = new char();
        char* pp = new char[10];
        char* ppp = (char*)malloc(10);
    }

    return 0;
}

  (10)其实知道了原理,自己写一套C++内存泄露检测也不难,主要是重载operator new和operator delete,可以把每次内存分配情况都记录在一个Map里,delete时删除记录,后程序退出时把map里没有delete的打印出来。当然我们知道Crt在实现new时一般实际上调的是malloc,而malloc可能又是调HeapAlloc,而HeapAlloc可能又是调用RtlAllocateHeap,所以理论上我们可以在这些函数的任意一层拦截和记录。但是如果你要实现自己的跨平台内存泄露检测,还是重载operator new吧。