C & C++

C & C++

Made by DeepSource

Found access to std::unique_ptr's raw pointer while deleting the pointer CXX-C2020

Style
Minor
Autofix

Accessing 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.

Bad Practice

std::unique_ptr<int> ptr(new int(42));
...
delete ptr.release();

Recommended

std::unique_ptr<int> ptr(new int(42));
...
ptr = nullptr;

or

std::unique_ptr<int> ptr(new int(42));
...
ptr.reset();