Muokkaa

Jaa


Error: alloc-dealloc-mismatch

Address Sanitizer Error: Mismatch between allocation and deallocation APIs

The alloc/dealloc mismatch functionality in AddressSanitizer is off by default for Windows. To enable it, run set ASAN_OPTIONS=alloc_dealloc_mismatch=1 before running the program. This environment variable is checked at runtime to report errors on malloc/delete, new/free, and new/delete[].

Example

// 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;
}

To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later developer command prompt:

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

Resulting error

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

See also

AddressSanitizer overview
AddressSanitizer known issues
AddressSanitizer build and language reference
AddressSanitizer runtime reference
AddressSanitizer shadow bytes
AddressSanitizer cloud or distributed testing
AddressSanitizer debugger integration
AddressSanitizer error examples