liveBook · Manning

Memory leak

As explained in 100 C++ Mistakes and How to Avoid Them

A memory leak is a failure to release dynamically allocated storage after it is no longer accessible or needed.

A memory leak occurs when dynamically allocated storage is no longer usable but remains unreleased. Long-running programs can accumulate leaks, reducing stability and available resources. section 6.3 section 7.6

Common C++ mistakes create leaks or make resource ownership unreliable:

  • Allocating with malloc and omitting the matching release leaves storage behind. section 6.3
  • Calling free on an object allocated with new skips destruction; mixing new with free produces undefined behavior. section 6.3
  • Deleting a derived object through a base pointer without a virtual destructor can leak derived resources. section 8.8
  • Copy constructors and assignment operators that share unique pointers can cause ownership errors; assignment must replace existing resources safely. section 7.6
Listing 6.5 Using malloc and free in an undisciplined manner
struct Buffer {
    char* str;
    Buffer(int size) : str(new char[size+1]) { str[0] = '\0'; }
    ~Buffer() { free(str); }
};
 
int main() {
    double* val = (double*)malloc(sizeof(int));                #1
    std::cout << val << '\n';
    Buffer* buf = (Buffer*)malloc(sizeof(Buffer));             #2
    std::cout << buf->str << ", size " << strlen(buf->str) << '\n';
    free(buf);
    return 0;
}

Compare the undisciplined allocation example with its corrected new/delete version to identify missing initialization, missing deletion, and mismatched release operations. section 6.3

Prefer ownership that pairs acquisition with release. Constructors should establish resource ownership, destructors should release it, and copy or move operations should preserve the ownership model. RAII and smart pointers reduce the opportunity for leaks, while a virtual destructor enables correct polymorphic cleanup. section 6.3 section 8.8 section 2.0 section 7.6

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Explains leaks through allocation, destruction, polymorphism, and copying, then stops short of leak-detection tooling.
Examples
Uses malloc/free and new/delete listings, a polymorphic Shape/Square hierarchy, and Page copy and unique_ptr examples.
What it assumes
Assumes basic C++ classes, pointers, constructors, and destructors while teaching resource ownership from concrete mistakes.
What you can do after
Diagnose/debug leaks by matching allocation with destruction and preserving ownership across inheritance, copying, assignment, and moves.
How it connects
Connects memory leaks with RAII, smart pointers, destructors, polymorphism, copy constructors, move semantics, and class invariants.

Also explained in

Compare how 5 books explain memory leak →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.