Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

RAII and resource ownership

RAII ties resource management to an object's lifetime: initialization establishes ownership, and the destructor releases the resource. A correctly designed owner therefore also performs cleanup during exception-driven stack unwinding.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

std::unique_ptr<T> owner = std::make_unique<T>(args...);

RAII (Resource Acquisition Is Initialization) is an idiom that ties resource management to an object's lifetime. Initialization establishes ownership, and the destructor releases the owned resource. A resource can be memory, a file, or a lock. Ownership means responsibility for release; a raw pointer or reference alone does not express that responsibility.

For constructed local objects with automatic storage duration, destructors run when their block is exited, including through return and during stack unwinding to a matching exception handler. Destruction occurs in reverse order of completed construction. If an exception escapes a non-delegating constructor, the destructor for the whole object does not run, but already constructed subobjects are destroyed. If an exception escapes the body of a delegating constructor after its target constructor has completed, however, the destructor for the whole object is invoked. Therefore, place resources under RAII ownership immediately, including in class members.

std::unique_ptr<T> represents exclusive ownership: it cannot be copied, but ownership can be moved. If it owns an object when destroyed, it invokes its deleter; the default deleter deletes that object. In the example, owner manages a dynamically created Resource. During stack unwinding, its destructor causes Resource to be destroyed, so released appears before caught. The messages only illustrate the lifecycle; they do not manage an external resource.

Resource-releasing destructors should not let exceptions escape; an exception escaping a destructor during stack unwinding leads to std::terminate. RAII does not mean an object is always on the stack, nor does it guarantee cleanup for every form of process termination. The example targets compilation as C++23; that standard is the repository compiler flag, not an attestation of a production runtime. The cited document is an evolving working draft, not a frozen C++23 publication.

Examples

#include <iostream>
#include <memory>

struct Resource {
    Resource() { std::cout << "acquired\n"; }
    ~Resource() noexcept { std::cout << "released\n"; }

    Resource(const Resource&) = delete;
    Resource& operator=(const Resource&) = delete;
};

int main() {
    try {
        auto owner = std::make_unique<Resource>();
        std::cout << "using\n";
        throw 42;
    } catch (int) {
        std::cout << "caught\n";
    }
}

Result

acquired
using
released
caught

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources