다음을 통해 공유


오류: alloc-dealloc-mismatch

삭제자 오류 해결: 할당 및 할당 취소 API 간의 불일치

alloc/dealloc Windows에서는 AddressSanitizer의 불일치 기능이 기본적으로 해제되어 있습니다. 사용하도록 설정하려면 프로그램을 실행하기 전에 실행 set ASAN_OPTIONS=alloc_dealloc_mismatch=1 합니다. 이 환경 변수는 런타임에 검사 , 및delete[]new/ .에 대한/freemallocdeletenew/ 오류를 보고합니다.

예시

// 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 오류 예제