64,654
社区成员
发帖
与我相关
我的任务
分享
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
的方式,导致重载new/delete时出错:#include <iostream>
#include <string>
using namespace std;
#ifdef _DEBUG
#define DEBUG_NEW new(_CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_NEW
#endif
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
class MemoryLeakChecker
{
public:
MemoryLeakChecker()
{
#ifdef _DEBUG
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
#endif // _DEBUG
}
~MemoryLeakChecker()
{
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif // _DEBUG
}
static MemoryLeakChecker& GetInstance()
{
static MemoryLeakChecker checker;
return checker;
}
};
class A
{
public:
A() { cout << "A" << endl; }
~A() { cout << "~A" << endl; }
void* operator new(size_t size)
{
return ::malloc(size);
}
void operator delete(void* p)
{
::free(p);
}
};
int main()
{
MemoryLeakChecker::GetInstance();
A* p = new A;
delete p;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_NEW
#endif
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
class MemoryLeakChecker
{
public:
MemoryLeakChecker()
{
#ifdef _DEBUG
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
#endif // _DEBUG
}
~MemoryLeakChecker()
{
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif // _DEBUG
}
static MemoryLeakChecker& GetInstance()
{
static MemoryLeakChecker checker;
return checker;
}
};
class A
{
public:
A() { cout << "A" << endl; }
~A() { cout << "~A" << endl; }
#ifdef _DEBUG
#define DEBUG_NEW_PARAMS , int type, const char* file, int line
#define DEBUG_DELETE_PARAMS , int, const char*, int
#else
#define DEBUG_NEW_PARAMS
#define DEBUG_DELETE_PARAMS
#endif
#pragma push_macro("new")
#undef new
static void* operator new(size_t size DEBUG_NEW_PARAMS)
{
return _malloc_dbg(size, type, file, line);
}
#pragma pop_macro("new")
#ifdef _DEBUG
static void operator delete(void* p DEBUG_DELETE_PARAMS)
{
_free_dbg(p, _NORMAL_BLOCK);
}
#endif
static void operator delete(void* p)
{
_free_dbg(p, _NORMAL_BLOCK);
}
};
int main()
{
MemoryLeakChecker::GetInstance();
A* p = new A;
return 0;
}
static void* operator new(size_t size, int type = _NORMAL_BLOCK, const char* file = __FILE__, int line = __LINE__)
{
return _malloc_dbg(size, type, file, line);
}
static void operator delete(void* p, int /*type*/, const char* /*file*/, int /*line*/)
{
_free_dbg(p, _NORMAL_BLOCK);
}
static void operator delete(void* p)
{
_free_dbg(p, _NORMAL_BLOCK);
}
class A
{
public:
A() { cout << "A" << endl; }
~A() { cout << "~A" << endl; }
static void* operator new(size_t size, int type = _NORMAL_BLOCK, const char* file = __FILE__, int line = __LINE__)
{
return _malloc_dbg(size, type, file, line);
}
static void operator delete(void* p, int /*type*/, const char* /*file*/, int /*line*/)
{
_free_dbg(p, _NORMAL_BLOCK);
}
static void operator delete(void* p)
{
_free_dbg(p, _NORMAL_BLOCK);
}
};
#pragma push_macro("new")
#undef new
#pragma pop_macro("new")
#include <iostream>
#include <string>
using namespace std;
#ifdef _DEBUG
#define DEBUG_NEW new(_CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_NEW
#endif
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
class MemoryLeakChecker
{
public:
MemoryLeakChecker()
{
#ifdef _DEBUG
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
#endif // _DEBUG
}
~MemoryLeakChecker()
{
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif // _DEBUG
}
static MemoryLeakChecker& GetInstance()
{
static MemoryLeakChecker checker;
return checker;
}
};
#pragma push_macro("new")
#undef new
class A
{
public:
A() { cout << "A" << endl; }
~A() { cout << "~A" << endl; }
static void* operator new(size_t size, int type = _CLIENT_BLOCK, const char* file = __FILE__, int line = __LINE__)
{
// cout << file << line;
return ::malloc(size);
}
static void operator delete(void* p, int type, const char* file, int line)
{
::free(p);
}
static void operator delete(void* p)
{
::free(p);
}
};
#pragma pop_macro("new")
int main()
{
MemoryLeakChecker::GetInstance();
A* p = new A;
delete p;
return 0;
}
class A
{
public:
A() { cout << "A" << endl; }
~A() { cout << "~A" << endl; }
public:
void* operator new(size_t size)
{
qpMallocNew(p, size);
return p;
}
void operator delete(void* p)
{
qpFreeDelete(p);
}
};
int main()
{
A* p = new A;
qpNewPtrEx(pi, int);
qpMallocEx(pm, float, 1);
qpNewArrayEx(pa, char, 100);
return 0;
}
/*
* Copyright (C) QPSOFT.COM All rights reserved.
*/
#pragma once
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
class MemoryLeakChecker
{
public:
MemoryLeakChecker()
{
#ifdef _DEBUG
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
#endif // _DEBUG
}
~MemoryLeakChecker()
{
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif // _DEBUG
}
static MemoryLeakChecker& GetInstance()
{
static MemoryLeakChecker checker;
return checker;
}
};
#ifdef _DEBUG
#define NEWCNT(PTR, TYPE) qp::Memory::GetInstance().New(PTR, __FILE__, __LINE__, TYPE)
#define DELETECNT(PTR, TYPE) qp::Memory::GetInstance().Delete(PTR, __FILE__, __LINE__, TYPE);
#else
#define NEWCNT(PTR, TYPE)
#define DELETECNT(PTR, TYPE)
#endif
#ifdef _DEBUG
#define qpNewPtr(P, T) P = ::new(std::nothrow) T; if (P == 0) qpDbgError(); else NEWCNT(P, 1);
#define qpNewPtrEx(P, T) T* P = ::new(std::nothrow) T; if (P == 0) qpDbgError(); else NEWCNT(P, 1);
#define qpNewArray(P, T, S) P = ::new(std::nothrow) T[S]; if (P == 0) qpDbgError(); else NEWCNT(P, 2);
#define qpNewArrayEx(P, T, S) T* P = ::new(std::nothrow) T[S]; if (P == 0) qpDbgError(); else NEWCNT(P, 2);
#define qpMalloc(P, T, S) P = (T*)::malloc((S) * sizeof(T)); if (P == 0) qpDbgError(); else NEWCNT(P, 3);
#define qpMallocEx(P, T, S) T* P = (T*)::malloc((S) * sizeof(T)); if (P == 0) qpDbgError(); else NEWCNT(P, 3);
#define qpMallocNew(P, S) void* P = ::malloc(S); if (P == 0) qpDbgError(); else NEWCNT(P, 4);
#define qpDelPtr(P) if (P != 0) { DELETECNT(P, 1); ::delete P; P = 0; }
#define qpDelArray(P) if (P != 0) { DELETECNT(P, 2); ::delete[] P; P = 0; }
#define qpFree(P) if (P != 0) { DELETECNT(P, 3); ::free(P); P = 0; }
#define qpFreeDelete(P) if (P != 0) { DELETECNT(P, 4); ::free(P); P = 0; }
#else
#define qpNewPtr(P, T) P = ::new(std::nothrow) T; if (P == 0) qpDbgError();
#define qpNewPtrEx(P, T) T* P = ::new(std::nothrow) T; if (P == 0) qpDbgError();
#define qpNewArray(P, T, S) P = ::new(std::nothrow) T[S]; if (P == 0) qpDbgError();
#define qpNewArrayEx(P, T, S) T* P = ::new(std::nothrow) T[S]; if (P == 0) qpDbgError();
#define qpMalloc(P, T, S) P = (T*)::malloc((S) * sizeof(T)); if (P == 0) qpDbgError();
#define qpMallocEx(P, T, S) T* P = (T*)::malloc((S) * sizeof(T)); if (P == 0) qpDbgError();
#define qpMallocNew(P, S) void* P = ::malloc(S); if (P == 0) qpDbgError();
#define qpDelPtr(P) if (P != 0) { ::delete P; P = 0; }
#define qpDelArray(P) if (P != 0) { ::delete[] P; P = 0; }
#define qpFree(P) if (P != 0) { ::free(P); P = 0; }
#define qpFreeDelete(P) if (P != 0) { ::free(P); P = 0; }
#endif
namespace qp
{
#ifdef _DEBUG
class Memory
{
struct MemoryData
{
MemoryData(const char* file, int line, int type) : file(file), line(line), type(type) {}
void* ptr;
std::string file;
int line;
int type;
};
public:
~Memory() { Analyze(); }
static Memory& GetInstance();
void New(void* ptr, const char* file, int line, int type);
void Delete(void* ptr, const char* file, int line, int type);
private:
void Analyze();
private:
typedef std::map<void*, MemoryData> MemoryMap;
MemoryMap m_memMap;
};
#endif
}
/*
* Copyright (C) QPSOFT.COM All rights reserved.
*/
#include <qp/Inc.h>
#include <qp/Qp.h>
#include <qp/Memory.h>
#include <qp/File.h>
using namespace qp;
#ifdef _DEBUG
Memory& Memory::GetInstance()
{
static Memory mem;
return mem;
}
void Memory::New(void* ptr, const char* file, int line, int type)
{
if (m_memMap.find(ptr) != m_memMap.end())
{
Debug::Printf("Memory Leak? %s, %d, %d", file, line, type);
qpASSERT(m_memMap.find(ptr) == m_memMap.end());
}
else
{
m_memMap.insert(std::make_pair(ptr, MemoryData(file, line, type)));
}
}
void Memory::Delete(void* ptr, const char* file, int line, int type)
{
MemoryMap::iterator it = m_memMap.find(ptr);
if (it == m_memMap.end())
{
Debug::Printf("Memory Leak? %s, %d, %d", file, line, type);
qpASSERT(it != m_memMap.end());
}
else
{
qpASSERT(it->second.type == type);
if (it->second.type == type)
{
m_memMap.erase(it);
}
}
}
void Memory::Analyze()
{
if (!m_memMap.empty())
{
File file;
file.Create(_T("MemoryReport.log"), File::CreateAlways);
int cnt = 0;
for (MemoryMap::iterator it = m_memMap.begin(); it != m_memMap.end(); ++it)
{
std::string buf(Global::Format("%d: %s, %d, %d\r\n", ++cnt, it->second.file.c_str(),
it->second.line, it->second.type));
file.Write(buf.c_str(), buf.size());
}
}
else
{
::DeleteFile(_T("MemoryReport.log"));
}
}
#endif
#pragma once
#define qpNew(PTR, TYPE) qp::Memory::GetInstance().New(PTR, __FILE__, __LINE__, TYPE)
#define qpDelete(PTR, TYPE) qp::Memory::GetInstance().Delete(PTR, TYPE);
#ifdef _DEBUG
#define qpNewPtr(P, T) P = ::new(std::nothrow) T; if (P == 0) qpDbgError(); else qpNew(P, 1);
#define qpNewPtrEx(P, T) T* P = ::new(std::nothrow) T; if (P == 0) qpDbgError(); else qpNew(P, 1);
#define qpNewArray(P, T, S) P = ::new(std::nothrow) T[S]; if (P == 0) qpDbgError(); else qpNew(P, 2);
#define qpNewArrayEx(P, T, S) T* P = ::new(std::nothrow) T[S]; if (P == 0) qpDbgError(); else qpNew(P, 2);
#define qpMalloc(P, T, S) P = (T*)::malloc((S) * sizeof(T)); if (P == 0) qpDbgError(); else qpNew(P, 3);
#define qpMallocEx(P, T, S) T* P = (T*)::malloc((S) * sizeof(T)); if (P == 0) qpDbgError(); else qpNew(P, 3);
#define qpDelPtr(P) if (P != 0) { qpDelete(P, 1); ::delete P; P = 0; }
#define qpDelArray(P) if (P != 0) { qpDelete(P, 2); ::delete[] P; P = 0; }
#define qpFree(P) if (P != 0) { qpDelete(P, 3); ::free(P); P = 0; }
#else
#define qpNewPtr(P, T) P = ::new(std::nothrow) T; if (P == 0) qpDbgError();
#define qpNewPtrEx(P, T) T* P = ::new(std::nothrow) T; if (P == 0) qpDbgError();
#define qpNewArray(P, T, S) P = ::new(std::nothrow) T[S]; if (P == 0) qpDbgError();
#define qpNewArrayEx(P, T, S) T* P = ::new(std::nothrow) T[S]; if (P == 0) qpDbgError();
#define qpMalloc(P, T, S) P = (T*)::malloc((S) * sizeof(T)); if (P == 0) qpDbgError();
#define qpMallocEx(P, T, S) T* P = (T*)::malloc((S) * sizeof(T)); if (P == 0) qpDbgError();
#define qpDelPtr(P) if (P != 0) { ::delete P; P = 0; }
#define qpDelArray(P) if (P != 0) { ::delete[] P; P = 0; }
#define qpFree(P) if (P != 0) { ::free(P); P = 0; }
#endif
namespace qp
{
class Memory
{
struct MemoryData
{
String& file;
int line;
int type;
};
public:
static Memory& GetInstance();
void New(void* ptr, const String& file, int line, int type);
void Delete(void* ptr, int type);
private:
typedef std::map<void*, MemoryData> MemoryMap;
MemoryMap m_memMap;
};
}