Skip to content

Commit 1829cfd

Browse files
authored
Merge pull request #5358 from Rageking8/fix-alloc-dealloc-pair-mistake-in-alloc-dealloc-mismatch-asan-error-reference
Fix alloc/dealloc pair mistake in `alloc-dealloc-mismatch` ASan error reference
2 parents d4a4f41 + ab87729 commit 1829cfd

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed
Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,68 @@
11
---
22
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
55
f1_keywords: ["alloc-dealloc-mismatch"]
66
helpviewer_keywords: ["alloc-dealloc-mismatch error", "AddressSanitizer error alloc-dealloc-mismatch"]
77
---
88
# Error: `alloc-dealloc-mismatch`
99

1010
> Address Sanitizer Error: Mismatch between allocation and deallocation APIs
1111
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.
1318

1419
## Example
1520

1621
```cpp
1722
// example1.cpp
18-
// alloc-dealloc-mismatch error
23+
// Demonstrate alloc-dealloc-mismatch error
1924
#include <stdio.h>
2025
#include <stdlib.h>
2126

22-
int main(int argc, char* argv[]) {
23-
27+
int main(int argc, char* argv[])
28+
{
2429
if (argc != 2) return -1;
2530

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;
3742
}
38-
3943
return 0;
4044
}
4145
```
4246
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`:
4448
4549
```cmd
4650
cl example1.cpp /fsanitize=address /Zi
4751
set ASAN_OPTIONS=alloc_dealloc_mismatch=1
4852
devenv /debugexe example1.exe 2
4953
```
5054

51-
### Resulting error
55+
### Output
5256

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":::
5458

5559
## See also
5660

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

Comments
 (0)