Skip to content

Commit 9626966

Browse files
committed
forgot to git add c26865.md
1 parent 201a051 commit 9626966

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

docs/code-quality/c26865.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 # TODO (@carsonradtke): I generated this with `PS > (New-Guid).ToString()`. Is that OK?
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+
## Remarks
14+
15+
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 is undefined behavior according to the C++ standard and the Microsoft C++ implementation.
16+
17+
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.
18+
19+
If memory is allocated with one family of allocation functions, it should be freed with a matching deallocation function.
20+
21+
C26865 covers the following allocation/deallocation pairs:
22+
23+
- C++ scalar new (`new`) must be deallocated with scalar delete (`delete`).
24+
- C++ array new (`new[]`) must be deallocated with array delete (`delete[]`).
25+
- C/C++ `malloc`/`calloc`/`realloc` must be deallocated with `free` (or `realloc`).
26+
- Windows `HeapAlloc` must be deallocated with `HeapFree`.
27+
- Windows `GlobalAlloc` must be deallocated with `GlobalFree`.
28+
- Windows `LocalAlloc` must be deallocated with `LocalFree`.
29+
- Windows `MIDL_user_allocate` must be deallocated with `MIDL_user_free`.
30+
- Component Object Model (COM) `CoTaskMemAlloc` must be deallocated with `CoTaskMemFree`.
31+
32+
Code analysis name: `ALLOCATION_DEALLOCATION_MISMATCH`
33+
34+
## Example
35+
36+
The following sample code generates warning C26865:
37+
38+
```cpp
39+
void f() {
40+
int *pInt = (int *)calloc(10, sizeof(int));
41+
// code ...
42+
delete pInt; // C26865: Memory allocated with 'calloc' is being deallocated with 'delete'. Use 'free' instead.
43+
}
44+
45+
void g() {
46+
char * str = new char[50];
47+
// code ...
48+
delete str; // C26865: Memory allocated with 'new[]' is being deallocated with 'delete'. Use 'delete[]' instead.
49+
}
50+
```
51+
52+
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).

0 commit comments

Comments
 (0)