Unique C ++ pointer: memory leak

I am a bit confused about the method of issuing a unique pointer. Here is my sample program.

class Test
{
public:
    Test(){std::cout << "ctor" << std::endl;}
    ~Test(){std::cout << "dtor" << std::endl;}
};

int main() {
    std::unique_ptr<Test> ptr(new Test());
    ptr.release(); // memory leak
    //ptr.reset(); // this is ok but not necessary
    return 0;
}

Conclusion:

ctor

Since it does not print dtor, I assume that it does not call the destructor Test, which will lead to a memory leak. Is it ?

+4
source share
1 answer

The word releasemeans "free ownership for the caller." So no, the destructor is not called by him.

, delete release d , reset, .
, , .

+11

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


All Articles