Does a unique_ptr leak cause a memory leak?

I'm confused at unique_ptr.release().

My goal is to infer the unique_ptr of the base class into a unique_ptrderived class.

So I found this question and answer

Derived *tmp = dynamic_cast<Derived*>(basePointer.get());
std::unique_ptr<Derived> derivedPointer;
if(tmp != nullptr)
{
    basePointer.release();
    derivedPointer.reset(tmp);
}

or

std::unique_ptr<Derived>
    derivedPointer(static_cast<Derived*>(basePointer.release()));

Then I was wondering what would happen to the base pointer after basePointer.release();.

Based on this question , I understand that it causes a memory leak.

I'm right?

+4
source share
2 answers

I'm right?

No.

The call release()does not miss anything, it just signals that you are in control of it.

If you missed a pointer after explicitly releasing it from the smart pointer, this is your mistake.

+12

, . tmp, , release(). , , .

+6

Source: https://habr.com/ru/post/1663263/


All Articles