Understanding VCL ownership and parentage

by Kent Reisdorph

The VCL has two elements that you need to understand; ownership and parentage. Ownership is the mechanism by which the VCL facilitates memory management. Ownership is determined by a form or component’s Owner property. Consider the case of a typical application where all forms are auto-created at application startup. When the VCL auto-creates the forms, it assigns the Application object as the owner of all forms. When the application is closed, the Application object runs through its list of owned forms and deletes each form. Similarly, a form owns all the components placed on the form. As each form is deleted, it deletes all of the components on the form. Ownership is specific to the class library—the VCL in this case. Windows itself does not have support for the concept of ownership.

Parentage deals with visual components contained on a form (or within another component such as a panel). Parentage is a Windows concept, not a VCL concept. Parentage is controlled in the VCL via the Parent property. Every windowed control (visual component) must have a parent. The parent window is the window that hosts a control. When a button is placed on a form, the form becomes the button’s parent. The Top and Left property of the button are specified relative to the parent’s top left corner.

To summarize, ownership deals with memory management, and parentage deals with the parent/child relationship between windows. In a C++Builder application, the form is the parent and the components placed on the form are the children.