エラー: alloc-dealloc-mismatch

Address Sanitizer のエラー: 割り当て API と割り当て解除 API の不一致

AddressSanitizer の alloc/dealloc 不一致機能は、Windows では既定でオフになっています。 有効にするには、プログラムを実行する前に set ASAN_OPTIONS=alloc_dealloc_mismatch=1 を実行します。 この環境変数は、malloc/deletenew/free、およびnew/delete[] に関するエラーを報告するためにランタイムでチェックされます。

// example1.cpp
// alloc-dealloc-mismatch error
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {

    if (argc != 2) return -1;

    switch (atoi(argv[1])) {

    case 1:
        delete[](new int[10]);
        break;
    case 2:
        delete (new int[10]);      // Boom!
        break;
    default:
        printf("arguments: 1: no error 2: runtime error\n");
        return -1;
    }

    return 0;
}

この例をビルドしてテストするには、Visual Studio 2019 バージョン 16.9 以降の開発者コマンド プロンプトで次のコマンドを実行します。

cl example1.cpp /fsanitize=address /Zi
set ASAN_OPTIONS=alloc_dealloc_mismatch=1
devenv /debugexe example1.exe 2

結果のエラー

Screenshot of debugger displaying alloc-dealloc-mismatch error in example 1.

関連項目

AddressSanitizer の概要
AddressSanitizer の既知の問題
AddressSanitizer のビルドと言語リファレンス
AddressSanitizer ランタイム リファレンス
AddressSanitizer シャドウ バイト
AddressSanitizer クラウドまたは分散テスト
AddressSanitizer デバッガーの統合
AddressSanitizer エラーの例