liveBook · Manning

Parent-child ownership

As explained in Fast by Default

Parent-child ownership is a resource-management pattern in which a parent object controls the lifetime of its child objects.

Parent-child ownership is a resource-management pattern in which a parent object controls the lifetime of its child objects. In Qt, each object can belong to a parent; destroying the parent automatically destroys the objects it owns. section 7.4.1

Apply the pattern with these safeguards:

  • Assign ownership deliberately when creating child objects. section 7.4.1
  • Ensure parents are eventually destroyed; a parent that remains alive retains every child indefinitely. section 7.4.1
  • Use ownership-aware analysis to identify which parent prevented cleanup, rather than relying only on generic leak reports. section 7.4.1

Persistent Qt memory growth

A long-lived Qt parent accumulates child objects as the application runs. Memory usage grows because destroying the parent—the event that would automatically destroy those children—never occurs. Qt Creator's memory analysis can help identify the responsible parent. section 7.4.1

How Fast by Default covers it

How deep it goes
Explains automatic child destruction and the leak caused by an undestroyed parent, then stops at diagnostic guidance.
Examples
Uses Qt objects retained by a parent and Qt Creator's ownership-aware memory analysis as concrete examples.
What you can do after
Diagnose retained Qt objects with Qt Creator's memory analysis and locate parents that prevent cleanup.
How it connects
Connects parent-child ownership with memory leaks, object lifetime, and memory profiling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast by Default. The section links point to the parts of the book it draws on.