About Debug

本文介绍如何使用Microsoft Visual C++中的调试宏和内存管理工具来检测和定位内存泄漏。提供了MFC中常用的调试宏用法,并推荐了MemoryManager工具,通过实例演示了如何在项目中应用此工具进行内存泄漏检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Detecting and Isolating Memory Leaks Using Microsoft Visual C++
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include "crtdbg.h"
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif
 
main
{
 // Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
 _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
}

MFC中一些用于调试的宏。
1.   ASSERT(a < 1);
2.   VERIFY(pWnd = GetDlgItem(IDC_MYEDIT));
3.   TRACE("Name: %s/n", obj.name());
4.   INFO("Name: %s/n", obj.name());
5.   DBG(AfxMessageBox("I'm here!"));
6.   BREAK();
 
评价很高的内存漏洞检测工具Memory Manager by Paul Nettle改进版
补充,链接一是作者原始发布的版本,只需要添加 mmgr.cpp, mmgr.h, nommgr.h 三个文件到项目即可;改进版(非原作者),则需要添加更多的文件,使用没有链接一方便。
个人使用经验:一、在所有的.cpp文件均加入"#ifndef NDEBUG#include "mmgr.h"#endif",放置在包含文件之末,因为该库很容易与其它系统库冲突。
                    二、不要在DLL源文件中使用该库,会造成使用该DLL的项目无法正常使用该库。
                    三、若日志文件中显示??:??等未知位置的内存泄露,那么可以锁定是在.h中的指针未释放。不将mmgr.h包含在头文件中,也是因为容易造成冲突问题。
### Release Mode vs Debug Mode Differences In programming, especially within environments like Visual C++ (VC), two primary build configurations exist: **Debug** and **Release** modes. These serve distinct purposes during development. #### Characteristics of Debug Mode The debug configuration is designed primarily for testing and troubleshooting code. This setup includes several features that facilitate easier debugging: - Detailed symbolic information embedded into executables or libraries. - Disabled optimizations which can obscure the relationship between source code and machine instructions[^1]. This allows developers to set breakpoints, inspect variables at runtime, step through lines of code one by one, etc., making it much simpler to identify issues early in the software lifecycle. #### Characteristics of Release Mode Conversely, release builds aim for optimal performance rather than ease-of-debugging capabilities. Key aspects include: - Full optimization enabled; this may involve reordering operations, removing redundant calculations, inline function expansion among others. - Minimal or no symbol table inclusion reduces executable size significantly compared with its counterpart. - Assertions are typically turned off unless explicitly specified otherwise. These changes result in faster execution times but make direct inspection more challenging due to lack of detailed metadata about program structure. ### Switching Between Modes To toggle between these settings in most Integrated Development Environments (IDEs): For projects using Microsoft's Visual Studio as an example: - Select `Build` from menu bar options. - Choose either `Configuration Manager`. - In Configuration dropdown list choose desired option (`Debug`, `Release`) accordingly before compiling project files again. Alternatively via command line tools provided alongside compilers such as MSBuild where appropriate flags control output type similarly. ```bash msbuild MyProject.sln /p:Configuration=Release ``` Or directly specifying target when invoking compiler commands manually if necessary. --related questions-- 1. What specific types of optimizations occur in a release build versus those present in debug? 2. How does enabling/disabling certain compile-time checks impact application stability across different versions? 3. Can you explain what happens internally when switching from debug to release compilation profiles? 4. Are there any best practices recommended while preparing applications specifically meant for production deployment?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值