_CrtSetBreakAlloc

在指定的对象分配序号上设置断点(仅限调试版本)。

语法

long _CrtSetBreakAlloc(
   long lBreakAlloc
);

参数

lBreakAlloc
为其设置断点的分配序号。

返回值

返回之前设置了断点的对象分配序号。

注解

_CrtSetBreakAlloc 允许应用程序通过在内存分配的特定点上中断并重新跟踪该请求的源,来执行内存泄露检测。 该函数使用在堆中分配它时分配到内存块的有序对象分配序号。 未定义 _DEBUG 时,会在预处理过程中删除对 _CrtSetBreakAlloc 的调用。

对象分配序号存储在 Crtdbg.h 中定义的 _CrtMemBlockHeader 结构的 lRequest 字段中。 当其中一个调试转储函数报告有关某个内存块的信息时,该编号将括在大括号中,例如 {36}。

若要详细了解如何将 _CrtSetBreakAlloc 与其他内存管理函数一起使用,请参阅跟踪堆分配请求。 若要详细了解如何在基堆的调试版本中分配、初始化和管理内存块,请参阅 CRT 调试堆详细信息

要求

例程 必需的标头
_CrtSetBreakAlloc <crtdbg.h>

有关兼容性的详细信息,请参阅 兼容性

仅限 C 运行时库的调试版本。

示例

// crt_setbrkal.c
// compile with: -D_DEBUG /MTd -Od -Zi -W3 /c /link -verbose:lib -debug

/*
* In this program, a call is made to the _CrtSetBreakAlloc routine
* to verify that the debugger halts program execution when it reaches
* a specified allocation number.
*/

#include <malloc.h>
#include <crtdbg.h>

int main( )
{
        long allocReqNum;
        char *my_pointer;

        /*
         * Allocate "my_pointer" for the first
         * time and ensure that it gets allocated correctly
         */
        my_pointer = malloc(10);
        _CrtIsMemoryBlock(my_pointer, 10, &allocReqNum, NULL, NULL);

        /*
         * Set a breakpoint on the allocation request
         * number for "my_pointer"
         */
        _CrtSetBreakAlloc(allocReqNum+2);

        /*
         * Alternate freeing and reallocating "my_pointer"
         * to verify that the debugger halts program execution
         * when it reaches the allocation request
         */
        free(my_pointer);
        my_pointer = malloc(10);
        free(my_pointer);
        my_pointer = malloc(10);
        free(my_pointer);
}

另请参阅

调试例程