Does unique_ptr::release() call the destructor?

前端 未结 5 873
后悔当初
后悔当初 2021-01-30 01:58

Is this code correct?

auto v =  make_unique(12);
v.release();     // is this possible?

Is it equivalent to delete of a

5条回答
  •  长情又很酷
    2021-01-30 02:37

    release will leak your raw pointer since you don't assign it to anything.

    It is meant to be used for something like

    int* x = v.release();
    

    Which means v is no longer managing the lifetime of that pointer, it is delegating the raw pointer ownership to x. If you just release without assigning to anything, you leak the raw pointer.

提交回复
热议问题