std::unique_ptr
will automatically call a deleter function when the unique_ptr
is destroyed. By default that deleter function applies the C++ delete
operator to the owned object which runs the destructor and frees the memory.
You can go wrong if you leak the unique_ptr
instance itself (then it never has a chance to perform its cleanup), if you detach your object from the unique_ptr
(that tells it not to do cleanup), or if you fail to match the deleter function to the original allocation method.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…