# std::unique_ptr<T,Deleter>::release

> C++ Atlas: `cppatlas.com/w/cpp/memory/unique_ptr/release` | Source: https://en.cppreference.com/w/cpp/memory/unique_ptr/release | Domain: Memory management | Standards: C++11

Releases the ownership of the managed object, if any.

## Official Specification Summary (cppreference)

```cpp
pointer release() noexcept; // (since C++11) (constexpr since C++23)
```

Releases the ownership of the managed object, if any.

`get()` returns `nullptr` after the call.

The caller is responsible for cleaning up the object (e.g. by use of `get_deleter()`).

### Parameters

(none)

### Return value

Pointer to the managed object or `nullptr` if there was no managed object, i.e. the value which would be returned by `get()` before the call.

### Example

```cpp
#include <cassert>
#include <iostream>
#include <memory>

struct Foo
{
    Foo() { std::cout << "Foo\n"; }
    ~Foo() { std::cout << "~Foo\n"; }
};

// Ownership of the Foo resource is transferred when calling this function
void legacy_api(Foo* owning_foo)
{
    std::cout << __func__ << '\n';
    // [legacy code that no one understands or dares touch anymore]
    // [...]
    delete owning_foo;
}

int main()
{
    std::unique_ptr<Foo> managed_foo(new Foo);
    // [code that might return or throw or some such]
    // [...]
    legacy_api(managed_foo.release());

    assert(managed_foo == nullptr);
}
```

Output:

```text
Foo
legacy_api
~Foo
```

### See also

- [`get`](https://en.cppreference.com/w/cpp/memory/unique_ptr/get): returns a pointer to the managed object (public member function)
- [`get_deleter`](https://en.cppreference.com/w/cpp/memory/unique_ptr/get_deleter): returns the deleter that is used for destruction of the managed object (public member function)
- [`reset`](https://en.cppreference.com/w/cpp/memory/unique_ptr/reset): replaces the managed object (public member function)

---
Attribution: content from https://en.cppreference.com/w/cpp/memory/unique_ptr/release (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.