C++ unique_ptr::release() 是否调用析构函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25609457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:22:30  来源:igfitidea点击:

Does unique_ptr::release() call the destructor?

c++unique-ptr

提问by Zeukis

Is this code correct?

这段代码正确吗?

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

Is it equivalent to deleteof a raw pointer?

它相当于delete原始指针吗?

回答by Mike Seymour

No, the code causes a memory leak. releaseis used to release ownership of the managed object withoutdeleting it:

不,该代码会导致内存泄漏。release用于释放托管对象的所有权而不删除它:

auto v = make_unique<int>(12);  // manages the object
int * raw = v.release();        // pointer to no-longer-managed object
delete raw;                     // needs manual deletion

Don't do this unless you have a good reason to juggle raw memory without a safety net.

除非您有充分的理由在没有安全网的情况下处理原始内存,否则不要这样做。

To delete the object, use reset.

要删除对象,请使用reset

auto v = make_unique<int>(12);  // manages the object
v.reset();                      // delete the object, leaving v empty

回答by Johann Gerell

Is this code correct?

这段代码正确吗?

No. Use std::unique_ptr<>::reset()to delete the internal raw pointer:

否。std::unique_ptr<>::reset()用于删除内部原始指针:

auto v =  std::make_unique<int>(12);
v.reset(); // deletes the raw pointer

After that is done, std::unique_ptr<>::get()will return nullptr(unless you provided a non-nullptrparameter to std::unique_ptr<>::reset()).

完成后,std::unique_ptr<>::get()将返回nullptr(除非您为 提供了非nullptr参数std::unique_ptr<>::reset())。

回答by JBL

Is this code correct?

这段代码正确吗?

It is not, and will leak.

它不是,而且会泄漏。

release()just let the calling code take back ownership of the memory that the unique_ptrheld until it was called. If you don't assign the pointer returned by release(), you'll just have a leak.

release()只需让调用代码收回在unique_ptr被调用之前持有的内存的所有权。如果您不分配由 返回的指针release(),则只会发生泄漏。

An explicit delete for a unique_ptrwould be reset(). But do remember that unique_ptrare there so that you don't have to manage directly the memory they hold. That is, you should know that a unique_ptrwill safely delete its underlying raw pointer once it goes out of scope.

a 的显式删除unique_ptrreset(). 但是请记住它们unique_ptr在那里,这样您就不必直接管理它们拥有的内存。也就是说,您应该知道 aunique_ptr一旦超出范围就会安全地删除其底层原始指针。

So you should have a very good reason to perform manual memory management on an automatic memory management object.

因此,您应该有充分的理由对自动内存管理对象执行手动内存管理。

回答by Cory Kramer

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

release将泄漏您的原始指针,因为您没有将它分配给任何东西。

It is meant to be used for something like

它旨在用于类似的东西

int* x = v.release();

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

这意味着v不再管理该指针的生命周期,而是将原始指针所有权委托给x。如果你只是release不分配任何东西,你就会泄漏原始指针。

回答by MichaelMoser

it may be a bit tricky for arbitrary types:

对于任意类型可能有点棘手:

  unique_ptr<Foo> v = get_me_some_foo();  // manages the object
  Foo * raw = v.release();        // pointer to no-longer-managed object
  delete raw;

is almost correct.

几乎是正确的。

  unique_ptr<Foo> v = get_me_some_foo();  // manages the object
  Foo * ptr = v.release();        // pointer to no-longer-managed object
  v.get_deleter() ( ptr );

this one would be correct in all situation; there may be a custom deleter defined on type Foo, but using the deleter returned by the unique_ptr object is good for all cases.

这在所有情况下都是正确的;可能在 Foo 类型上定义了一个自定义删除器,但使用 unique_ptr 对象返回的删除器适用于所有情况。