|
| 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). |
0 commit comments