| title | C26401 | |
|---|---|---|
| ms.date | 07/21/2017 | |
| ms.topic | conceptual | |
| f1_keywords |
|
|
| helpviewer_keywords |
|
|
| ms.assetid | b9d3d398-697a-4a5d-8bfe-9c667dffb90b | |
| description | CppCoreCheck rule that enforces C++ Core Guidelines I.11 |
This check detects places where moving to owner<T> can be a good option for the first stage of refactoring. Like C26400 it enforces rules I.11 and R.3, but focuses on the "release" portion of the pointer lifetime. It warns on any call to operator delete if its target is neither an owner<T> nor an implicitly assumed owner. For more information, see C26400 regarding the auto declarations. This does include expressions that refer to global variables, formals, and so on.
Warnings C26400 and C26401 always occur with C26409, but they are more appropriate for scenarios where immediate migration to smart pointers is not feasible. In such cases the owner<T> concept can be adopted first and C26409 may be temporarily suppressed.
struct myStruct {};
myStruct* createMyStruct();
void function()
{
myStruct* pMyStruct = createMyStruct();
// ...
delete pMyStruct; // C26401. Do not delete a raw pointer that is not an owner<T>
}See that C26401 is removed if ownership of the pointer is indicated by gsl::owner.
#include <gsl/pointers>
struct myStruct {};
gsl::owner<myStruct*> createMyStruct();
void function()
{
gsl::owner<myStruct*> pMyStruct = createMyStruct();
// ...
delete pMyStruct; // no warning.
}