Redigera

Dela via


Warning C26404

Do not delete an owner<T> which may be in invalid state (r.3)

Remarks

Once an owner pointer releases or transfers its resource, it gets into an "invalid" state. Deleting such a pointer may lead to immediate memory corruption due to double delete, or to an access violation when the deleted resource is accessed from another owner pointer.

Code analysis name: DONT_DELETE_INVALID

Example 1

Deleting an owner after transferring its value:

gsl::owner<State*> validState = nullptr;
gsl::owner<State*> state = ReadState();
validState = state;
if (!IsValid(state))
    delete state;   // C26404

Example 2

Deleting an uninitialized owner:

gsl::owner<Message*> message;
if (popLast)
    message = ReleaseMessage();
delete message; // C26404