Failures and antipatterns · Intermediate

Double frees

Trace competing release responsibilities for one allocation and align ownership transfer with a single cleanup contract.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. One allocation cannot be released twice
  2. Trace success and failure cleanup joins
  3. Distinguish an address from an allocation lifetime
  4. Represent and transfer a single owner
  5. Verify release behavior on exceptional paths
  6. Ask Logeia about this articleCheck your understanding

One allocation cannot be released twice

A double free releases the same allocation more than once during one lifetime. The problem is not merely that free appears in two functions, but that their responsibilities overlap for the same object. An allocator may detect the error immediately or its internal state may be damaged. This differs from accessing an object after release, although both defects can originate in confused ownership. In C, free(NULL) is a no-op, but passing a previously released non-null pointer again is not valid. Trace the allocation and its owner instead of assuming that a familiar cleanup function makes a repeated call harmless.

Trace success and failure cleanup joins

For a hypothetical example, F allocates P and passes it to helper G. If G’s contract releases P on failure while F also releases it in common cleanup, the failure branch returns the allocation twice. Happy-path testing can miss the error, making partial initialization and error joins important review targets. This is an ownership example rather than an incident log. Specify whether G borrows P, takes ownership, and whether ownership transfer occurs on failure as well as success. A patch intended to close a leak can introduce double release when cleanup is added without revisiting this contract. Connect each exit path with exactly the responsibility it retains.

Distinguish an address from an allocation lifetime

Connect the original allocation, first release and second release using matching build and symbol information. A detector such as AddressSanitizer can provide these paths, but removing only the final failing free can leave the earlier ownership mistake unexplained. The same numerical address appearing twice in a log is not automatically the same lifetime: an allocator can reuse it after a new allocation. Conversely, different variable names can alias the same object. Pointer-only logging is therefore incomplete evidence. Include allocation identity or generation where appropriate, object type, call path and completion ordering to distinguish duplicate release from legitimate reuse of an address.

Represent and transfer a single owner

Possible repairs include scoped owners, ownership-carrying types and interfaces that make a moved-from owner unusable for release. Setting one variable to null does not update aliases held elsewhere. Shared ownership also requires a correct count and destruction contract rather than multiple manual decrements for one reference. Match allocation and deallocation families, including scalar and array destruction where applicable. A lock can serialize concurrent operations, but does not automatically prevent two sequential releases caused by a wrong contract. Synchronization and lifetime are separate responsibilities. The design should state who is allowed to perform the final release and how all other paths know they no longer own it.

Verify release behavior on exceptional paths

After the repair, exercise ordinary completion, partial initialization failure, repeated callbacks, cancellation, reentry and paths where several cleanup mechanisms converge. Explain which branch owns the allocation after a successful transfer and which branch retains it when transfer never occurs. Deleting every release may hide the crash by introducing a leak. Verify that each resource is eventually returned once and that references do not survive that lifetime. Separate detector coverage from unexecuted paths in the record. The strongest result is an ownership contract that makes duplicate cleanup inexpressible or explicitly rejected, supported by failure-path tests rather than merely by disappearance of one allocator report.

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