Skip to content

Commit 21d7edd

Browse files
Merge pull request #6061 from carsonRadtke/carsonradtke/alloc-dealloc-mismatch-docs
[dev18] new docs for new alloc/dealloc mismatch warning
2 parents e018969 + 147b67b commit 21d7edd

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

docs/code-quality/c26865.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
description: "Learn more about: Warning C26865"
3+
title: Warning C26865
4+
ms.date: 10/03/2022
5+
f1_keywords: ["C26865", "ALLOCATION_DEALLOCATION_MISMATCH", "__WARNING_ALLOCATION_DEALLOCATION_MISMATCH"]
6+
helpviewer_keywords: ["C26865"]
7+
ms.assetid: 2fbe9dc5-fa43-47b9-97a7-3f8215da1d40
8+
---
9+
# Warning C26865
10+
11+
> Memory allocated with '\<allocation-function\>' is being deallocated with '\<wrong-deallocation-function\>'. Use '\<correct-deallocation-function\>' instead.
12+
13+
This rule was added in Visual Studio 2026 18.0.
14+
15+
## Remarks
16+
17+
This warning indicates that the memory was allocated with one family of allocation functions, but was freed with a deallocation function from a different family. This usage results in undefined behavior according to C/C++ and the Microsoft MSVC implementation.
18+
19+
The exact ramifications of this defect are difficult to predict. It might result in leaks for classes with destructors that perform memory deallocation. It could cause inconsistent behavior for classes with destructors that perform semantically significant operations, or memory corruptions and crashes. In other cases the mismatch might be unimportant, depending on the implementation of the compiler and its libraries. Analysis tools can't always distinguish between these situations.
20+
21+
If memory is allocated with one family of allocation functions, it should be freed with a matching deallocation function.
22+
23+
C26865 covers the following allocation/deallocation pairs:
24+
25+
- C++ scalar new (`new`) must be deallocated with scalar delete (`delete`).
26+
- C++ array new (`new[]`) must be deallocated with array delete (`delete[]`).
27+
- C/C++ `malloc`/`calloc`/`realloc` must be deallocated with `free` (or `realloc`).
28+
- Windows `HeapAlloc` must be deallocated with `HeapFree`.
29+
- Windows `GlobalAlloc` must be deallocated with `GlobalFree`.
30+
- Windows `LocalAlloc` must be deallocated with `LocalFree`.
31+
- Windows `MIDL_user_allocate` must be deallocated with `MIDL_user_free`.
32+
- Component Object Model (COM) `CoTaskMemAlloc` must be deallocated with `CoTaskMemFree`.
33+
34+
Code analysis name: `ALLOCATION_DEALLOCATION_MISMATCH`
35+
36+
## Example
37+
38+
The following sample code generates warning C26865:
39+
40+
```cpp
41+
void f() {
42+
int *pInt = (int *)calloc(10, sizeof(int));
43+
// code ...
44+
delete pInt; // C26865: Memory allocated with 'calloc' is being deallocated with 'delete'. Use 'free' instead.
45+
}
46+
47+
void g() {
48+
char * str = new char[50];
49+
// code ...
50+
delete str; // C26865: Memory allocated with 'new[]' is being deallocated with 'delete'. Use 'delete[]' instead.
51+
}
52+
```
53+
54+
Manual memory management has many pitfalls in terms of memory leaks and exceptions. To avoid these kinds of potential leaks altogether, use the mechanisms that are provided by the C++ Standard Library (STL). These mechanisms include [`shared_ptr`](../standard-library/shared-ptr-class.md), [`unique_ptr`](../standard-library/unique-ptr-class.md), and containers such as [`vector`](../standard-library/vector.md). For more information, see [Smart pointers](../cpp/smart-pointers-modern-cpp.md) and [C++ Standard Library](../standard-library/cpp-standard-library-reference.md).

docs/code-quality/c6278.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ helpviewer_keywords: ["C6278"]
99

1010
> '*variable*' is allocated with array new [], but deleted with scalar delete. Destructors will not be called.
1111
12+
## Notice
13+
14+
Warning C6278 was removed in Visual Studio 2026. Use the more generic warning [`C26865`](../code-quality/C26865.md) instead.
15+
1216
## Remarks
1317

1418
This warning appears only in C++ code and indicates that the calling function has inconsistently allocated memory with the array **`new []`** operator, but freed it with the scalar **`delete`** operator. This usage is undefined behavior according to the C++ standard and the Microsoft C++ implementation.

docs/code-quality/c6279.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ helpviewer_keywords: ["C6279"]
99

1010
> '*variable-name*' is allocated with scalar new, deleted with array delete []
1111
12+
This warning appears only in C++ code and indicates that the calling function has inconsistently allocated memory with the scalar `new` operator, but freed it with the array `delete[]` operator. If memory is allocated with scalar `new`, it should typically be freed with scalar `delete`.
13+
14+
## Notice
15+
16+
Warning C6279 was removed in Visual Studio 2026. Use the more generic warning [`C26865`](../code-quality/C26865.md) instead.
17+
1218
## Remarks
1319

1420
This warning appears only in C++ code and indicates that the calling function has inconsistently allocated memory with the scalar `new` operator, but freed it with the array `delete[]` operator. If memory is allocated with scalar `new`, it should typically be freed with scalar `delete`.

docs/code-quality/c6280.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ helpviewer_keywords: ["C6280"]
99

1010
> '*variable-name*' is allocated with '*function-name-1*', but deleted with '*function-name-2*'
1111
12+
This warning indicates that the calling function has inconsistently allocated memory by using a function from one family and freed it by using a function from another.
13+
14+
## Notice
15+
16+
Warning C6280 was removed in Visual Studio 2026. Use the more generic warning [`C26865`](../code-quality/C26865.md) instead.
17+
1218
## Remarks
1319

1420
This warning indicates that the calling function has inconsistently allocated memory by using a function from one family and freed it by using a function from another.

docs/code-quality/c6283.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ helpviewer_keywords: ["C6283"]
99

1010
> '*variable-name*' is allocated with array new [], but deleted with scalar delete
1111
12+
This warning appears only in C++ code and indicates that the calling function has inconsistently allocated memory with the array `new []` operator, but freed it with the scalar `delete` operator.
13+
14+
## Notice
15+
16+
Warning C6283 was removed in Visual Studio 2026. Use the more generic warning [`C26865`](../code-quality/C26865.md) instead.
17+
1218
## Remarks
1319

1420
This warning appears only in C++ code and indicates that the calling function has inconsistently allocated memory with the array `new []` operator, but freed it with the scalar `delete` operator.

docs/code-quality/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ items:
661661
href: ../code-quality/c26863.md
662662
- name: Warning C26864
663663
href: ../code-quality/c26864.md
664+
- name: Warning C26865
665+
href: ../code-quality/c26865.md
664666
- name: Warning C28020
665667
href: ../code-quality/c28020.md
666668
- name: Warning C28021

0 commit comments

Comments
 (0)