About DEBUG_NEW

一、在调试模式下,new操作符号通过宏定义转换成了调试版本。

在文件头经常可以发现以下语句:

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

其中,将new定义为DEBUG_NEW

二、DEBUG_NEW的处理

调试版本的new操作函数:void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine);

宏替换:#define DEBUG_NEW new(THIS_FILE, __LINE__) //文件名、行号被传入,供调试输出。

实际代码如下:

void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
{
return ::operator new(nSize, _NORMAL_BLOCK, lpszFileName, nLine);
}
//分配内存
void* __cdecl operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine)
{
#ifdef _AFX_NO_DEBUG_CRT
UNUSED_ALWAYS(nType);
UNUSED_ALWAYS(lpszFileName);
UNUSED_ALWAYS(nLine);
return ::operator new(nSize);
#else
void* pResult;
#ifdef _AFXDLL
_PNH pfnNewHandler = _pfnUninitialized;
#endif
for (;;)
{
pResult = _malloc_dbg(nSize, nType, lpszFileName, nLine);
if (pResult != NULL)
return pResult;
#ifdef _AFXDLL
if (pfnNewHandler == _pfnUninitialized)
{
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
pfnNewHandler = pState-> m_pfnNewHandler;
}
if (pfnNewHandler == NULL || (*pfnNewHandler)(nSize) == 0)
break;
#else
if (_afxNewHandler == NULL || (*_afxNewHandler)(nSize) == 0)
break;
#endif
}
return pResult;
#endif
}
#endif //_DEBUG

 

DEBUG_NEW

  #define new DEBUG_NEW

  说明:

  帮助查找内存错误。用户在程序中使用DEBUG_NEW,用户通常使用new运算符来从堆上分配。在Debug模式下(但定义了一个DEBUG符号),DEBUG_NEW为它分配的每个对象记录文件名和行号。然后,在用户使用CMemoryState::DumpAllObjectSince成员函数时,每个以DEBUG_NEW分配的对象分配的地方显示出文件名和行号。 为了使用DEBUG_NEW,应在用户的资源文件中插入以下指令: #define new DEBUG_NEW 一旦用户插入本指令,预处理程序将在使用new的地方插入DEBUG_NEW,而MFC作其余的工作。但用户编译自己的程序的一个发行版时,DEBUG_NEW便进行简单的new操作,而且不产生文件名和行号消息。

 

以下是MSDN中的内容:   
在   MFC   中,可以使用   DEBUG_NEW   宏代替   new   运算符来帮助定位内存泄漏。在程序的“Debug”版本中,DEBUG_NEW   将为所分配的每个对象跟踪文件名和行号。当编译程序的“Release”版本时,DEBUG_NEW   将解析为不包含文件名和行号信息的简单   new   操作。因此,在程序的“Release”版本中不会造成任何速度损失。   
如果不想重写整个程序来使用   DEBUG_NEW   代替   new,则可以在源文件中定义下面的宏:   
#define   new   DEBUG_NEW  
当进行对象转储时,用   DEBUG_NEW   分配的每个对象均将显示被分配到的文件和行号,使您可以查明内存泄漏源。   
MFC   框架的“Debug”版本自动使用   DEBUG_NEW,但代码不自动使用它。如果希望利用   DEBUG_NEW   的好处,则必须显式使用   DEBUG_NEW   或   #define   new,如上所示。   

转载于:https://www.cnblogs.com/taoxu0903/archive/2008/07/25/1251579.html

Here are some tips that could help you in your journey to learn Java: 1. Get Familiar with the Basics: Start with the basics of Java syntax, data types, variables, operators, control structures, loops, and functions. 2. Practice, Practice, Practice: The more you practice, the better you get. Write code to solve simple problems, and gradually increase the complexity of the problems you tackle. 3. Use Online Resources: There are many online resources available for learning Java, including online tutorials, video courses, and forums. Take advantage of these resources to deepen your understanding of the language. 4. Join a Study Group: Joining a study group can be a great way to stay motivated and learn from others. You can also learn from others’ mistakes and share your own experiences. 5. Read Java Documentation: The official Java documentation is a wealth of information, and it’s a great resource for learning the ins and outs of the language. 6. Work on Projects: Choose a project that interests you, and work on it until it’s complete. This will help you put into practice everything you’ve learned and also give you a sense of accomplishment. 7. Debugging: Learn to debug your code effectively. This will help you find and fix bugs quickly and easily, and improve your overall programming skills. 8. Stay Up-to-Date: Java is constantly evolving, and new features and libraries are added regularly. Stay up-to-date with the latest developments in the Java world to improve your skills and keep your knowledge fresh. Remember, learning Java takes time and effort, but with persistence and determination, you can become a proficient Java programmer.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值