_CrtSetDbgFlag
来源:互联网 发布:全国老师手机号数据 编辑:程序博客网 时间:2024/05/18 02:59
若要了解有关 Visual Studio 2017 RC 的最新文档,请参阅 Visual Studio 2017 RC 文档。
检索或修改的状态_crtDbgFlag标志来控制调试堆管理器 (仅限调试版本) 的分配行为。
int _CrtSetDbgFlag( int newFlag );
参数
newFlag
新状态的_crtDbgFlag。
返回的前一状态_crtDbgFlag。
_CrtSetDbgFlag
函数允许应用程序来控制调试堆管理器通过修改的位域跟踪内存分配的方式_crtDbgFlag标志。 通过设置位(打开),该应用程序可指示调试堆管理器执行特殊的调试操作,包括在应用程序退出时检查内存泄露并报告是否找到任何内存泄露、通过指定已释放的内存块应保留在堆的链接列表中来模拟内存不足情况,以及通过在每次分配请求时检查每个内存块来验证该堆的完整性。 当_DEBUG未定义,则调用_CrtSetDbgFlag
在预处理过程中删除。
下表列出的位域_crtDbgFlag并说明它们的行为。 因为设置位将导致诊断输出增加、程序执行速度减慢,因此在默认情况下不会设置这些位(已关闭)。 有关详细信息,有关这些位域,请参阅堆状态报告函数。
_CLIENT_BLOCK
。 关闭︰ 将新的分配添加到堆的链接列表,但设置阻止类型设置为_IGNORE_BLOCK。还可以与任何堆频率检查宏组合。_CRTDBG_CHECK_ALWAYS_DF关闭打开︰ 调用_CrtCheckMemory在每次分配和解除分配请求。 关闭:必须显式调用
_CrtCheckMemory
。设置此标志后,堆频率检查宏不会产生任何影响。
_CRTDBG_CHECK_CRT_DF
关闭打开:包括泄漏检测和内存状态差异操作中的 _CRT_BLOCK
类型。 关闭:这些操作将忽略运行时库在内部使用的内存。还可以与任何堆频率检查宏组合。_CRTDBG_DELAY_FREE_MEM_DF关闭打开︰ 将已释放的内存块在堆链接列表中,将它们分配_FREE_BLOCK键入,然后使用字节值 0xDD 填充它们。 关闭:不要将已释放的块保留在堆链接列表中。
还可以与任何堆频率检查宏组合。
_CRTDBG_LEAK_CHECK_DF
关闭打开︰ 执行自动泄露检查在程序退出时通过调用_CrtDumpMemoryLeaks ,则生成错误报告,如果应用程序未能释放其所分配的所有内存。 关闭:不要在程序退出时自动执行泄露检查。还可以与任何堆频率检查宏组合。
堆频率检查宏
您可以指定何种频率 C 运行库执行的调试堆的验证 (_CrtCheckMemory
) 基于对的调用次数malloc
,realloc
,免费,和_msize
。
然后,_CrtSetDbgFlag
将检查某个值的 newFlag
参数较高的 16 位。 指定的值是数malloc
,realloc
,免费,和_msize
调用之间_CrtCheckMemory
调用。 为此,将提供四个预定义的宏。
默认情况下,_CrtCheckMemory
将调用一次调用的每个 1024 次malloc
, realloc
,免费,和_msize
。
例如,可以指定 1 次堆检查每个 16 malloc
, realloc
,免费,和_msize
操作替换为以下代码︰
#include <crtdbg.h> int main( ) { int tmp; // Get the current bits tmp = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Clear the upper 16 bits and OR in the desired freqency tmp = (tmp & 0x0000FFFF) | _CRTDBG_CHECK_EVERY_16_DF; // Set the new bits _CrtSetDbgFlag(tmp); }
在指定 _CRTDBG_CHECK_ALWAYS_DF 时,忽略 newFlag
参数较高的 16 位。 在这种情况下,_CrtCheckMemory
称为您每次调用malloc
,realloc
,免费,和_msize
。
newFlag
是要应用于的新状态_crtDbgFlag也是每位域的值的组合。
更改一个或多个位域并创建标志的新状态
调用
_CrtSetDbgFlag
与newFlag
等于_CRTDBG_REPORT_FLAG
以获取当前_crtDbgFlag状态,并在临时变量中存储返回的值。通过对带有相应位掩码的临时变量(在应用程序代码中由清单常量显示)进行
OR
运算来打开任何位。请关闭其他位由AND运算结果的变量进行按位不相应位掩码。
调用
_CrtSetDbgFlag
与newFlag
要设置的新状态的临时变量中存储的值等于_crtDbgFlag。
以下代码演示如何通过在堆链接列表中保留已释放的内存块来模拟内存不足情况,以及如何阻止在每个分配请求时都调用 _CrtCheckMemory
:
// Get the current state of the flag // and store it in a temporary variable int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); // Turn On (OR) - Keep freed memory blocks in the // heap's linked list and mark them as freed tmpFlag |= _CRTDBG_DELAY_FREE_MEM_DF; // Turn Off (AND) - prevent _CrtCheckMemory from // being called at every allocation request tmpFlag &= ~_CRTDBG_CHECK_ALWAYS_DF; // Set the new state for the flag _CrtSetDbgFlag( tmpFlag );
有关内存管理和调试堆的概述,请参阅CRT 调试堆详细信息。
若要禁用有标志_CrtSetDbgFlag
函数,您应该AND的变量进行的按位不的位掩码。
如果newFlag
不是有效的值,此函数将调用无效参数处理程序,如中所述参数验证。 如果允许执行继续,则该函数会将errno
设置为 EINVAL
并返回之前的 _crtDbgFlag
状态。
_CrtSetDbgFlag
<crtdbg.h>有关更多兼容性信息,请参阅“简介”中的 Compatibility 。
调试版本C 运行时库仅。
// crt_crtsetdflag.c // compile with: /c -D_DEBUG /MTd -Od -Zi -W3 /link -verbose:lib /debug /* * This program concentrates on allocating and freeing memory * blocks to test the functionality of the _crtDbgFlag flag.. */ #include <string.h> #include <malloc.h> #include <crtdbg.h> int main( ) { char *p1, *p2; int tmpDbgFlag; _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE ); _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDERR ); /* * Set the debug-heap flag to keep freed blocks in the * heap's linked list - This will allow us to catch any * inadvertent use of freed memory */ tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); tmpDbgFlag |= _CRTDBG_DELAY_FREE_MEM_DF; tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF; _CrtSetDbgFlag(tmpDbgFlag); /* * Allocate 2 memory blocks and store a string in each */ p1 = malloc( 34 ); p2 = malloc( 38 ); strcpy_s( p1, 34, "p1 points to a Normal allocation block" ); strcpy_s( p2, 38, "p2 points to a Client allocation block" ); /* * Free both memory blocks */ free( p2 ); free( p1 ); /* * Set the debug-heap flag to no longer keep freed blocks in the * heap's linked list and turn on Debug type allocations (CLIENT) */ tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); tmpDbgFlag |= _CRTDBG_ALLOC_MEM_DF; tmpDbgFlag &= ~_CRTDBG_DELAY_FREE_MEM_DF; _CrtSetDbgFlag(tmpDbgFlag); /* * Explicitly call _malloc_dbg to obtain the filename and * line number of our allocation request and also so we can * allocate CLIENT type blocks specifically for tracking */ p1 = _malloc_dbg( 40, _NORMAL_BLOCK, __FILE__, __LINE__ ); p2 = _malloc_dbg( 40, _CLIENT_BLOCK, __FILE__, __LINE__ ); strcpy_s( p1, 40, "p1 points to a Normal allocation block" ); strcpy_s( p2, 40, "p2 points to a Client allocation block" ); /* * _free_dbg must be called to free the CLIENT block */ _free_dbg( p2, _CLIENT_BLOCK ); free( p1 ); /* * Allocate p1 again and then exit - this will leave unfreed * memory on the heap */ p1 = malloc( 10 ); }
不适用。 若要调用标准 C 函数,请使用 PInvoke
。 有关更多信息,请参见 平台调用示例。
调试例程
_crtDbgFlag
_CrtCheckMemory
- _CrtSetDbgFlag
- _CrtSetDbgFlag检测内存泄露
- _CrtSetDbgFlag函数简介
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- 使用_CrtSetDbgFlag检测内存泄露
- vc使用_CrtSetDbgFlag 检测内存泄漏
- c/c++的内存泄露查找 _CrtSetDbgFlag()
- 设置windows服务器计划任务定时执行PHP文件详细步骤
- Redis-Keys的通用操作
- English summary of November
- ora-01555处理
- Android富文本编辑器:XRichText
- _CrtSetDbgFlag
- SSZiparchive unknown type of name 'unz_global_info' 等
- 够疯狂!比特币突破 10,000 美元大关,年初不到 1000 $
- 【Scikit-Learn 中文文档】监督学习:从高维观察预测输出变量
- 硬盘又坏了?硬盘数据恢复的工具有这些
- 趣图:合并分支的拟人化
- Java | 基本类型
- Spring中获取xml与properties配置文件的值
- 欢迎使用CSDN-markdown编辑器