Yeda AI Tips · #099

Español

A Raw Owning Pointer in C++ Is a Review Blocker

A raw owning pointer in C++ is a review-blocking bug. Not a style nit, not a "we'll clean it up later" — a blocker. Manual memory management without a wrapper is exactly where leaks, double frees, and use-after-free bugs hide, and it is the single easiest red flag to enforce mechanically: if a T* owns what it points to, the review stops until it doesn't.

This matters twice as much now that coding agents write a large share of your diffs. Models trained on decades of pre-C++11 code will happily emit new in one function and delete in another. Your job in review is not to trace every path between them — it's to reject the pattern outright.

Why "owning" is the keyword

A raw pointer itself is fine. The C++ Core Guidelines are explicit: R.3 — a raw pointer (a T*) is non-owning. The pointer that observes an object someone else owns is idiomatic and zero-cost. The bug class starts the moment a raw pointer is also responsible for calling delete:

That's why Core Guideline R.11 says avoid calling new and delete explicitly at all in application code.

The replacement table

You wroteWrite insteadWhy
T* p = new T(...) + delete pauto p = std::make_unique<T>(...)Exclusive ownership, freed on every exit path (R.20)
"Two places need it alive"std::shared_ptr<T> — only for genuinely shared lifetimeRefcounting has a cost; R.21 says prefer unique_ptr
Pointer + length parametersstd::span<T> (C++20)A bounds-aware non-owning view (F.24 / I.13)
Just looking, never freeingT*, T&, or const T&Non-owning by convention (R.3, F.7)
Hand-written destructor/copy/moveRule of zero — let members manage themselvesIf every member is RAII, you write none of the five (C.20)

The decision procedure fits in one sentence: unique by default, shared only when lifetime is genuinely shared, views for everything else.

Power tricks: enforcing it on agents

The payoff is the reel's closer: ownership becomes explicit, and an entire bug class stops being expressible. When your agent emits a raw owning pointer — block it.

Resources

Building an AI feature? Yeda AI designs, audits, and ships production LLM systems.

Talk to us · Read the blog