std::unique_ptr
's raw pointer while deleting the pointer CXX-C2020Accessing the raw pointer of a std::unique_ptr
and calling delete
on it is unnecessary as std::unique_ptr
provides API to do it safely.
To fix this issue, replace delete <unique_ptr>.release()
with <unique_ptr> = nullptr
. This is a shorter and simpler way to reset the std::unique_ptr
and ensures that the object is properly deallocated.
std::unique_ptr<int> ptr(new int(42));
...
delete ptr.release();
std::unique_ptr<int> ptr(new int(42));
...
ptr = nullptr;
or
std::unique_ptr<int> ptr(new int(42));
...
ptr.reset();