錯誤: alloc-dealloc-mismatch

位址清理程式錯誤:配置與解除配置 API 不符

alloc/dealloc Windows 預設會關閉 AddressSanitizer 中的不相符功能。 若要啟用它,請在執行程式之前執行 set ASAN_OPTIONS=alloc_dealloc_mismatch=1 。 此環境變數會在執行時間檢查,以報告 、 newfree/ 和 delete[]/new 上的 malloc/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 錯誤範例