Failures and antipatterns · Intermediate

Use-after-free

Trace references that survive deallocation and repair ownership using the allocation, release and later-access sequence.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. A remaining address does not preserve lifetime
  2. Trace delayed asynchronous work
  3. Connect allocation, release and access evidence
  4. Repair ownership and cancellation contracts
  5. Separate reproduced paths from untested behavior
  6. Ask Logeia about this articleCheck your understanding

A remaining address does not preserve lifetime

Use-after-free occurs when code accesses dynamically allocated storage after the object’s lifetime has ended. The numerical pointer can remain unchanged, and an immediate read may even appear to return an old value, but neither establishes a valid object. An allocator can reuse the region for something else, making the symptom appear later or in a different thread. A crash is not required for the execution to be incorrect. Distinguish this defect from a leak, in which storage is not released. The central question is which component owns the lifetime and which references are allowed to remain valid at each step.

Trace delayed asynchronous work

For a hypothetical schedule, create request object R and give its address to a callback scheduled for later. The caller releases R, then a timer or completion event executes the callback with that stale reference. This illustrates an ordering error rather than reports a real incident. List registration, ownership transfer, cancellation request, completion, release and final access. A cancellation function returning does not necessarily mean a callback already executing has finished. Inspect the actual API contract. Reviewing only the immediate call stack can miss the reference retained by asynchronous work and the operation that eventually consumes it.

Connect allocation, release and access evidence

A detector such as AddressSanitizer can report the invalid access along with allocation and deallocation stacks. Use matching build information and symbols to connect all three paths to one lifetime. Patching only the function where the process finally crashed can leave the early release or duplicate ownership unchanged. Preserve the input and concurrency conditions used for the reproduction. Allocation layout changes can hide a symptom, so a normal build failing to reproduce it is not evidence of a repair. Conversely, a clean instrumented test covers the paths it executed rather than proving every possible callback schedule safe.

Repair ownership and cancellation contracts

Repair the lifetime contract through scoped ownership, deliberately shared ownership where appropriate, or a borrowing rule that remains valid until completion. Locking only the act of reading a pointer is insufficient if another thread can reclaim the object immediately after the lock is released. Setting one pointer variable to null does not clear aliases or copies held by callbacks. Reference counting has its own conditions, including correct count updates and handling ownership cycles. Specify whether unregistering waits for in-flight work or transfers ownership to it. Apply the same release rules to exceptions, cancellation and ordinary completion instead of adding unrelated null checks around the visible failure.

Separate reproduced paths from untested behavior

After the repair, exercise early exit, exception, timeout, immediate cancellation and completion-versus-unregistration schedules as well as the happy path. Extending lifetime can introduce unwanted retention, so inspect leaks too. Keep the reproduction isolated and record the report, input, build settings and revised ownership rule. A falling crash rate is useful operational evidence but does not replace explaining why the invalid access order can no longer occur. Memory-safe languages or ownership checking can prevent classes of these defects, while foreign-function and manually managed boundaries still require review. The outcome should be an explicit, testable lifetime relationship rather than dependence on favorable allocator timing.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 22

Choose a learning path