c++内存泄露检测,长文慎入! (一)

2014-11-24 02:32:56 · 作者: · 浏览: 4

关于内存泄露的,今天无意想到,网上找了一下


文中的memcheck晚点的时候在把它打包成dll

来个测试工程:

[cpp]
#include
#include "string"
#include "vector"
using namespace std;

int main()
{
{
char *str;
str = new char[100 + 1];

strcpy(str, "zengraoli");

cout << str << endl;
}

_CrtDumpMemoryLeaks(); // 内存泄露检测

return 0;
}

#include
#include "string"
#include "vector"
using namespace std;

int main()
{
{
char *str;
str = new char[100 + 1];

strcpy(str, "zengraoli");

cout << str << endl;
}

_CrtDumpMemoryLeaks(); // 内存泄露检测

return 0;
}


Ctrl+F5后,在Dbgview.exe中出现了下面的信息:

\

Detected memory leaks

这个是提示

再次修改:
[cpp]
#include "stdafx.h"
#include
#include "string"
#include "vector"
using namespace std;

#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include
#include
#define newEx new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

inline void EnableMemLeakCheck()
{
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}

int _tmain(int argc, _TCHAR* argv[])
{
EnableMemLeakCheck();

char *str = newEx char[9 + 1];

cout << str << endl;

return 0;
}

#include "stdafx.h"
#include
#include "string"
#include "vector"
using namespace std;

#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include
#include
#define newEx new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

inline void EnableMemLeakCheck()
{
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}

int _tmain(int argc, _TCHAR* argv[])
{
EnableMemLeakCheck();

char *str = newEx char[9 + 1];

cout << str << endl;

return 0;
}


Ctrl+F5后,在Dbgview.exe中出现了下面的信息:

\

可以看到正是25行的地方导致内存泄露的

Code没来得及细看,能用就是了^_^,方便就行,枉自猜测一下原理----------重载了new和delete,对他俩进行一个计数,并记下行数,两个不为偶数,则就是代表已经出现内存泄露了


MemCheck.h:
[cpp]
#ifndef MEMCHECK_H
#define MEMCHECK_H
#include // for size_t

// Hijack the new operator (both scalar and array versions)
void* operator new(std::size_t, const char*, long);
void* operator new[](std::size_t, const char*, long);
#define new new (__FILE__, __LINE__)

extern bool traceFlag;
#define TRACE_ON() traceFlag = true
#define TRACE_OFF() traceFlag = false

extern bool activeFlag;
#define MEM_ON() activeFlag = true
#define MEM_OFF() activeFlag = false

#endif

#ifndef MEMCHECK_H
#define MEMCHECK_H
#include // for size_t

// Hijack the new operator (both scalar and array versions)
void* operator new(std::size_t, const char*, long);
void* operator new[](std::size_t, const char*, long);
#define new new (__FILE__, __LINE__)

extern bool traceFlag;
#define TRACE_ON() traceFlag = true
#define TRACE_OFF() traceFlag = false

extern bool activeFlag;
#define MEM_ON() activeFlag = true
#define MEM_OFF() activeFlag = false

#endif

MemCheck.cpp:
[cpp]
#include
#include
#include
using namespace std;
#undef new

// Global flags set by macros in MemCheck.h
bool traceFlag = true;
bool activeFlag = false;

namespace
{
// Memory map entry type
struct Info
{
void* ptr;
const char* file;
long line;
};

// Memory map data
const size_t MAXPTRS = 10000u;
Info memMap[MAXPTRS];
size_t nptrs = 0;

// Searches the map for an address
int findPtr(void* p)
{
for (int i = 0; i < nptrs; ++i)
{
if (memMap[i].ptr == p)
{
return i;
}
}
return -1;
}

void delPtr