Skip to content

Latest commit

 

History

History
64 lines (49 loc) · 2.15 KB

error-alloc-dealloc-mismatch.md

File metadata and controls

64 lines (49 loc) · 2.15 KB
title description ms.date f1_keywords helpviewer_keywords
Error: alloc-dealloc-mismatch
Source examples and live debug screenshots for alloc-dealloc-mismatch errors.
03/02/2021
alloc-dealloc-mismatch
alloc-dealloc-mismatch error
AddressSanitizer error alloc-dealloc-mismatch

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

:::image type="content" source="media/alloc-dealloc-mismatch-example-1.png" alt-text="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