You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Heap reallocation functions don't free the passed buffer if reallocation is unsuccessful, potentially resulting in a memory leak if not handled properly. To correct the issue, assign the result of the reallocation function to a temporary location, and then replace the original pointer after successful reallocation.
16
+
Heap reallocation functions don't free the passed buffer if reallocation is unsuccessful, potentially resulting in a memory leak if not handled properly. To correct the issue, assign the result of the reallocation function to a temporary variable, and then replace the original pointer after successful reallocation.
17
17
18
18
Code analysis name: REALLOCLEAK
19
19
20
20
## Example
21
21
22
-
The following sample code generates this warning. This issue stems from the assignment of `x` to the return value from realloc. If realloc fails and returns a NULL pointer, then `x` will fail to be freed:
22
+
The following sample code generates warning C6308. This issue stems from the assignment of the return value from `realloc` to `x`. If `realloc` fails and returns a null pointer, then the original memory pointed to by `x` won't be freed:
23
23
24
24
```cpp
25
25
#include<malloc.h>
@@ -37,7 +37,7 @@ void f( )
37
37
}
38
38
```
39
39
40
-
To resolve the issue, you can create a temporary variable to store the return status of realloc. This change allows you to free the previously allocated memory safely if realloc fails:
40
+
To resolve the issue, you can create a temporary variable to store the return value of `realloc`. This change allows you to free the previously allocated memory safely if `realloc` fails:
41
41
42
42
```cpp
43
43
#include <malloc.h>
@@ -61,8 +61,8 @@ void f()
61
61
62
62
This warning might generate noise if there's a live alias to the buffer-to-be-reallocated at the time of the assignment of the result of the reallocation function.
63
63
64
-
To avoid these kinds of issues altogether, you can use the mechanisms that are provided by the C++ Standard Template Library (STL). These include [shared_ptr](../standard-library/shared-ptr-class.md), [unique_ptr](../standard-library/unique-ptr-class.md), and [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).
64
+
To avoid these kinds of issues altogether, you can use the mechanisms that are provided by the C++ Standard Template Library (STL). These include [`shared_ptr`](../standard-library/shared-ptr-class.md), [`unique_ptr`](../standard-library/unique-ptr-class.md), and [`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