[[moreEffC++]] has a chapter dedicated to resource leaks in dtor. I have yet to read it, but here are my views:
A “Resource” means a heap object as a data member, in every case I know. In such a case, “Leak” means failure to call q[ delete ] on the data member.
To guarantee the q[ delete ], I feel one of the simplest yet most reliable strategies is a smart ptr as a data member. In particular
- If a base-class subobject is already fully constructed when subclass ctor throws, the base-class dtor would run. Any resource in base-class is released if using smart ptr.
- If a component subobject is already fully constructed when host ctor throws, the component dtor would run.
- In the normal case of a fully constructed subclass or host object, then obviously its dtor would trigger the base-class dtor or component class dtor, in reverse order
However, replacing every ptr field with a smart ptr is costly and impractical.