|
1 | 1 | --- |
2 | 2 | title: "Error: alloc-dealloc-mismatch" |
3 | | -description: "Source examples and live debug screenshots for alloc-dealloc-mismatch errors." |
4 | | -ms.date: 03/02/2021 |
| 3 | +description: "Learn about the alloc-dealloc-mismatch Address Sanitizer error." |
| 4 | +ms.date: 05/28/2025 |
5 | 5 | f1_keywords: ["alloc-dealloc-mismatch"] |
6 | 6 | helpviewer_keywords: ["alloc-dealloc-mismatch error", "AddressSanitizer error alloc-dealloc-mismatch"] |
7 | 7 | --- |
8 | 8 | # Error: `alloc-dealloc-mismatch` |
9 | 9 |
|
10 | 10 | > Address Sanitizer Error: Mismatch between allocation and deallocation APIs |
11 | 11 |
|
12 | | -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[]`. |
| 12 | +Enables runtime detection of mismatched memory operations that may lead to undefined behavior, such as: |
| 13 | +- `malloc` must be paired with `free`, not `delete` or `delete[]`. |
| 14 | +- `new` must be paired with `delete`, not `free` or `delete[]`. |
| 15 | +- `new[]` must be paired with `delete[]`, not `delete` or `free`. |
| 16 | + |
| 17 | +In Windows, `alloc-dealloc-mismatch` error detection is off by default. To enable it, set the environment variable `set ASAN_OPTIONS=alloc_dealloc_mismatch=1` before running your program. |
13 | 18 |
|
14 | 19 | ## Example |
15 | 20 |
|
16 | 21 | ```cpp |
17 | 22 | // example1.cpp |
18 | | -// alloc-dealloc-mismatch error |
| 23 | +// Demonstrate alloc-dealloc-mismatch error |
19 | 24 | #include <stdio.h> |
20 | 25 | #include <stdlib.h> |
21 | 26 |
|
22 | | -int main(int argc, char* argv[]) { |
23 | | - |
| 27 | +int main(int argc, char* argv[]) |
| 28 | +{ |
24 | 29 | if (argc != 2) return -1; |
25 | 30 |
|
26 | | - switch (atoi(argv[1])) { |
27 | | - |
28 | | - case 1: |
29 | | - delete[](new int[10]); |
30 | | - break; |
31 | | - case 2: |
32 | | - delete (new int[10]); // Boom! |
33 | | - break; |
34 | | - default: |
35 | | - printf("arguments: 1: no error 2: runtime error\n"); |
36 | | - return -1; |
| 31 | + switch (atoi(argv[1])) |
| 32 | + { |
| 33 | + case 1: |
| 34 | + delete[](new int[10]); |
| 35 | + break; |
| 36 | + case 2: |
| 37 | + delete (new int[10]); // Boom! |
| 38 | + break; |
| 39 | + default: |
| 40 | + printf("arguments: 1: no error 2: runtime error\n"); |
| 41 | + return -1; |
37 | 42 | } |
38 | | - |
39 | 43 | return 0; |
40 | 44 | } |
41 | 45 | ``` |
42 | 46 |
|
43 | | -To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts): |
| 47 | +In a Visual Studio 2019 version 16.9 or later [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts), run the following commands to see an exampe of `alloc_dealloc_mismatch`: |
44 | 48 |
|
45 | 49 | ```cmd |
46 | 50 | cl example1.cpp /fsanitize=address /Zi |
47 | 51 | set ASAN_OPTIONS=alloc_dealloc_mismatch=1 |
48 | 52 | devenv /debugexe example1.exe 2 |
49 | 53 | ``` |
50 | 54 |
|
51 | | -### Resulting error |
| 55 | +### Output |
52 | 56 |
|
53 | | -:::image type="content" source="media/alloc-dealloc-mismatch-example-1.png" alt-text="Screenshot of debugger displaying alloc-dealloc-mismatch error in example 1."::: |
| 57 | +:::image type="content" source="media/alloc-dealloc-mismatch-example-1.png" alt-text="Screenshot of debugger displaying alloc-dealloc-mismatch error in example 1." lightbox="media/alloc-dealloc-mismatch-example-1.png"::: |
54 | 58 |
|
55 | 59 | ## See also |
56 | 60 |
|
57 | | -[AddressSanitizer overview](./asan.md)\ |
58 | | -[AddressSanitizer known issues](./asan-known-issues.md)\ |
59 | | -[AddressSanitizer build and language reference](./asan-building.md)\ |
60 | | -[AddressSanitizer runtime reference](./asan-runtime.md)\ |
61 | | -[AddressSanitizer shadow bytes](./asan-shadow-bytes.md)\ |
62 | | -[AddressSanitizer cloud or distributed testing](./asan-offline-crash-dumps.md)\ |
63 | | -[AddressSanitizer debugger integration](./asan-debugger-integration.md)\ |
64 | | -[AddressSanitizer error examples](./asan-error-examples.md) |
| 61 | +[AddressSanitizer overview](asan.md)\ |
| 62 | +[AddressSanitizer known issues](asan-known-issues.md)\ |
| 63 | +[AddressSanitizer build and language reference](asan-building.md)\ |
| 64 | +[AddressSanitizer runtime reference](asan-runtime.md)\ |
| 65 | +[AddressSanitizer shadow bytes](asan-shadow-bytes.md)\ |
| 66 | +[AddressSanitizer cloud or distributed testing](asan-offline-crash-dumps.md)\ |
| 67 | +[AddressSanitizer debugger integration](asan-debugger-integration.md)\ |
| 68 | +[AddressSanitizer error examples](asan-error-examples.md) |
0 commit comments