LESSON 01 / OWNERSHIP

Someone has to clean up.

Your AI allocated it. Your comment won’t free it. Someone needs an actual job.

2 MIN READ + QUIZJump to reality check ↓

The mess

Your AI leaves a “remember to free this” comment. Adorable. Comments are famously terrible garbage collectors.

Ownership means responsibility for cleanup. Think of a borrowed apartment key: knowing the address doesn't make you the landlord. A raw pointer stores an address; it doesn't tell you who must release the resource.

void process() {
    auto* buffer = new int[64];
    if (!ready()) return; // The comment will clean up. Surely.
    delete[] buffer;
}

If ready() returns false, the pointer disappears and the allocation stays. You've invented a donation scheme for RAM.

The fix

Use std::vector<int> for this buffer: it owns its storage and releases it on destruction. For a single separately owned object, consider std::unique_ptr.

Several functions can borrow an object without owning it. Use std::shared_ptr only for actual shared ownership; strong reference cycles can prevent cleanup. “Shared everywhere” is a custody dispute, not a design.

Ask your AI

Name each resource's owner. Show cleanup on success, early return, and exceptions. Use an owning type; justify any shared ownership.

No named owner? No convincing cleanup story. Reject the hand-waving.

Receipts: C++ Core Guidelines — resource management.

THE REALITY CHECK

Did that actually stick?

Three questions. “The AI said so” is not an answer.

1 / 3Two raw pointers, same address. How many owners does that prove?
2 / 3An early return skips manual cleanup. What needs fixing?
3 / 3The AI uses shared_ptr everywhere. Problem solved?

Choose one answer for each question.

KEEP GOING. ALL LESSONS ARE OPEN.Your pointer outlived its welcome. →