Edit

Share via


Warning C26403

Reset or explicitly delete an owner<T> pointer 'variable' (r.3)

Owner pointers are like unique pointers: they own a resource exclusively, and manage release of the resource, or its transfers to other owners. This check validates that a local owner pointer properly maintains its resource through all execution paths in a function. If the resource wasn't transferred to another owner, or wasn't explicitly release, the checker warns, and points to the declaration of the pointer variable.

For more information, see the C++ Core Guidelines.

Remarks

  • Currently this check doesn't give the exact path that fails to release the resource. This behavior may be improved in future releases. It may be difficult to find exact ___location for a fix. The better approach is to try to replace plain pointers in complex functions with unique pointers to avoid any risks.

  • The check may discard an over-complicated function in order to not block code analysis. Generally, the complexity of functions should be maintained under some reasonable threshold. We may consider adding a local complexity check to the C++ Core Guidelines module if there's clear demand for it. This limitation is applicable to other rules that are sensitive to data flow.

  • The warning may fire on clearly false positive cases where memory is deleted only after the null check of a pointer. These false positives are the result of a current limitation of the tool's API, but it may be improved in future.

Code analysis name: RESET_OR_DELETE_OWNER

Example

Missing cleanup during error handling:

gsl::owner<int*> sequence = GetRandomSequence(); // C26403

try
{
    StartSimulation(sequence);
}
catch (const std::exception& e)
{
    if (KnownException(e))
        return; // Skipping the path which deletes the owner.

    ReportException(e);
}

delete [] sequence;