Share via


_CrtSetBreakAlloc

更新 : 2007 年 11 月

指定されたオブジェクト割り当て順序番号に、ブレークポイントを設定します (デバッグ バージョンだけ)。

long _CrtSetBreakAlloc( 
   long lBreakAlloc 
);

パラメータ

  • lBreakAlloc
    ブレークポイントを設定する割り当て順序番号。

戻り値

ブレークポイントが設定されていた前回のオブジェクト割り当て番号を返します。

解説

_CrtSetBreakAlloc を使用すると、アプリケーションは、特定のメモリ割り当て位置にブレークポイントを設定し、要求元までトレースすることによって、メモリ リークを検出できます。使用されるオブジェクト割り当て順序番号は、ヒープへの割り当て時にメモリ ブロックに割り当てられたシーケンシャル番号です。_DEBUG が未定義の場合、_CrtSetBreakAlloc の呼び出しはプリプロセスで削除されます。

オブジェクト割り当て順序番号は、Crtdbg.h で定義されている _CrtMemBlockHeader 構造体の lRequest フィールドに格納されます。デバッグ ダンプ関数のいずれかが出力するメモリ ブロック情報のレポートでは、中かっこで囲まれて {36} のように表示されます。

ほかのメモリ管理関数と連携した _CrtSetBreakAlloc の使い方の詳細については、「ヒープ割り当て要求の追跡」を参照してください。

必要条件

ルーチン

必須ヘッダー

_CrtSetBreakAlloc

<crtdbg.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

ライブラリ

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);
        _crtBreakAlloc = 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);
}

.NET Framework の相当するアイテム

適用できません。標準 C 関数を呼び出すには、PInvoke を使用します。詳細については、「プラットフォーム呼び出しの例」を参照してください。

参照

参照

デバッグ ルーチン