Skip to content

Instantly share code, notes, and snippets.

@zxteloiv
Created March 3, 2016 01:31
Show Gist options
  • Save zxteloiv/0994e8e86010cbae6ffd to your computer and use it in GitHub Desktop.
Save zxteloiv/0994e8e86010cbae6ffd to your computer and use it in GitHub Desktop.
C++ memory leak detection header. Got from Kingsoft many years ago.
/* -------------------------------------------------------------------------
// 文件名 : debugheader.h
// 创建者 :
// 创建时间 : 2008-3-21 12:06:48 pm
// 功能描述 :
//
// $Id: $
// -----------------------------------------------------------------------*/
#ifndef __DEBUGHEADER_H__
#define __DEBUGHEADER_H__
// -------------------------------------------------------------------------
#include <crtdbg.h>
#include <assert.h>
#ifndef _DEBUG
#define PROCESS_ERROR(x) \
do \
{ \
if(!(x)) \
goto Exit0 ; \
}while(0)
#else
#define PROCESS_ERROR(x) \
if (!(x)) \
{ \
assert(false); \
goto Exit0; \
} \
NULL;
#endif //_DEBUG
#define RETURN_ON_FAIL(exp) \
do { \
if (!(exp)) \
{ \
assert(false); \
return false; \
} \
} while(0)
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
#include "debugnew.h"
#ifdef _DEBUG
#define TRACE(msg) \
do { \
_CrtDbgReport(_CRT_WARN, NULL, NULL, NULL, "%s", msg); \
} while(0)
// #define TRACE0(msg) \
// do { \
// _CrtDbgReport(_CRT_WARN, __FILE__, __LINE__, NULL, "%s", msg); \
// } while(0)
// #define TRACE1(msg, arg1) \
// do { \
// _CrtDbgReport(_CRT_WARN, __FILE__, __LINE__, NULL, msg, arg1); \
// } while(0)
// #define TRACE2(msg, arg1, arg2) \
// do { \
// _CrtDbgReport(_CRT_WARN, __FILE__, __LINE__, NULL, msg, arg1, arg2); \
// } while(0)
// #define TRACE3(msg, arg1, arg2, arg3) \
// do { \
// _CrtDbgReport(_CRT_WARN, __FILE__, __LINE__, NULL, msg, arg1, arg2, arg3); \
// } while(0)
#define TRACE0(msg) \
do { \
TCHAR szMsg[2048]; \
wsprintf(szMsg, _T("%s(%d) : %s"), _T(__FILE__), __LINE__, msg); \
OutputDebugString(szMsg); \
} while(0)
#define BARETRACE0(msg) \
do { \
OutputDebugString(msg); \
} while(0)
#define TRACE1(msg, arg1) \
do { \
TCHAR szMsg1[256], szMsg2[2048]; \
wsprintf(szMsg1, msg, arg1); \
wsprintf(szMsg2, _T("%s(%d) : %s"), _T(__FILE__), __LINE__, szMsg1); \
OutputDebugString(szMsg2); \
} while(0)
#define BARETRACE1(msg, arg1) \
do { \
TCHAR szMsg[256]; \
wsprintf(szMsg, msg, arg1); \
OutputDebugString(szMsg); \
} while(0)
#define TRACE2(msg, arg1, arg2) \
do { \
TCHAR szMsg1[256], szMsg2[2048]; \
wsprintf(szMsg1, msg, arg1, arg2); \
wsprintf(szMsg2, _T("%s(%d) : %s"), _T(__FILE__), __LINE__, szMsg1); \
OutputDebugString(szMsg2); \
} while(0)
#define BARETRACE2(msg, arg1, arg2) \
do { \
TCHAR szMsg[256]; \
wsprintf(szMsg, msg, arg1, arg2); \
OutputDebugString(szMsg); \
} while(0)
#define TRACE3(msg, arg1, arg2, arg3) \
do { \
TCHAR szMsg1[256], szMsg2[2048]; \
wsprintf(szMsg1, msg, arg1, arg2, arg3); \
wsprintf(szMsg2, _T("%s(%d) : %s"), _T(__FILE__), __LINE__, szMsg1); \
OutputDebugString(szMsg2); \
} while(0)
#define BARETRACE3(msg, arg1, arg2, arg3) \
do { \
TCHAR szMsg[256]; \
wsprintf(szMsg, msg, arg1, arg2, arg3); \
OutputDebugString(szMsg); \
} while(0)
#define TRACE0_ONCE(msg) \
do { \
static int nCount = 0; \
if (0 == nCount) \
{ \
nCount++; \
TRACE0(msg); \
} \
} while(0)
#define ASSERT_ONCE(f) \
do { \
static int nCount = 0; \
if (!f && 0 == nCount) \
{ \
nCount++; \
assert(FALSE); \
} \
} while(0)
#define VERIFY(f) assert(f)
#else // #ifdef _DEBUG
#define TRACE(msg)
#define TRACE0(msg)
#define TRACE1(msg, arg1)
#define TRACE2(msg, arg1, arg2)
#define TRACE3(msg, arg1, arg2, arg3)
#define TRACE0_ONCE(msg)
#define ASSERT_ONCE(f)
#define VERIFY(f) ((void)(f))
#endif // #ifdef _DEBUG
// -------------------------------------------------------------------------
// $Log: $
#endif /* __DEBUGHEADER_H__ */
/* -------------------------------------------------------------------------
// 文件名 : debugnew.h
// 创建者 : 金山训练营
// 创建时间 : 2007-12-29
// 功能描述 : 辅助检查内存泄漏
//
// 修订记录:
// 1. 创建: 2007.12.29.
//
// 说明: 若使用了MFC则不必使用本文件. MFC有自己的检查内存泄露机制.
//
// 使用步骤:
// 1. include此文件.
//
// 2. 在.cpp中增加以下定义:
// #ifdef _DEBUG
// #define new DEBUG_NEW
// #endif
//
// 3. 在程序退出后, 检查VC的output窗口内是否有dump memory leak
//
// $Id: $
// -----------------------------------------------------------------------*/
#ifndef __DEBUGNEW_H__
#define __DEBUGNEW_H__
#ifdef __cplusplus
#ifndef _DEBUG
// release版下不做任何事情
#else
// debug版下重定义new, 以便当出现内存泄漏时自动报警
#include <crtdbg.h>
// #ifndef TCHAR
// #if defined(_UNICODE) || defined(UNICODE)
// typedef wchar_t TCHAR;
// #else
// typedef char TCHAR;
// #endif
// #endif
inline void * __cdecl operator new(unsigned int size,
const char *file, int line)
{
int nFlag = _CrtSetDbgFlag (_CRTDBG_REPORT_FLAG);
nFlag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag (nFlag);
return ::operator new(size, _NORMAL_BLOCK, file, line);
};
#define DEBUG_NEW new(__FILE__, __LINE__)
//#define new DEBUG_NEW
// 若不同时定义delete, 则在编译时会出现warning
inline void __cdecl operator delete(void* pData,
const char* /* lpszFileName */,
int /* nLine */)
{
::operator delete(pData);
}
#endif // #ifndef _DEBUG
#endif // #ifdef __cplusplus
#endif // #ifndef __DEBUGNEW_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment