TForm.Create(AOwner)

Picking the right parameter to optimize memory usage

When you create Delphi objects dynamically that inherit from TControl, such as a TForm (representing a form/window in Delphi applications), the constructor "Create" expects an "Owner" parameter:

constructor Create(AOwner: TComponent) ;

The AOwner parameter is the owner of the TForm object. The owner of the form is responsible for freeing the form -- i.e., memory allocated by the form -- when needed. The form appears in the Components array of its owner and it is destroyed automatically when its owner is destroyed. 

You have three choices for the AOwner parameter: Nil, self, and application.

To understand the answer, you first need to know the meaning of "nil," "self" and "Application."

  • Nil specifies that no object owns the form and therefore the developer is responsible for freeing the created form (by calling myForm.Free when you no longer need the form)
  • Self specifies the object in which the method is called. If, for example, you are creating a new instance of a TMyForm form from inside a button's OnClick handler (where this button is placed on a MainForm), self refers to "MainForm." Thus, when the MainForm is freed, it will also free MyForm.
  • Application specifies a global TApplication type variable created when you run your application. "Application" encapsulates your application as well as providing many functions that occur in the background of the program.

Examples:

  1. Modal forms. When you create a form to be displayed modally and freed when the user closes the form, use "nil" as the owner:
    var myForm : TMyForm; begin  myForm := TMyForm.Create(nil) ;  try    myForm.ShowModal;  finally    myForm.Free; end; end;
  2. Modeless forms. Use "Application" as the owner:
    var
    myForm : TMyForm;
    ...
    myForm := TMyForm.Create(Application) ;

Now, when you terminate (exit) the application, the "Application" object will free the "myForm" instance.

Why and when is TMyForm.Create(Application) NOT recommended? If the form is a modal form and will be destroyed, you should pass "nil" for the owner.

You could pass "application," but the time delay caused by the notification method being sent to every component and form owned or indirectly owned by the Application could prove disruptive. If your application consists of many forms with many components (in the thousands), and the form you're creating has many controls (in the hundreds), the notification delay can be significant.

Passing "nil" as the owner instead of "application" will cause the form to appear sooner, and will not otherwise affect the code.

However, if the form you need to create is not modal and is not created from the application's main form, then when you specify "self" as the owner, closing the owner will free the created form. Use "self" when you don't want the form to outlive its creator.

Warning: To dynamically instantiate a Delphi component and explicitly free it sometime later, always pass "nil" as the owner. Failure to do so can introduce unnecessary risk, as well as performance and code maintenance problems.

In SDI applications, when a user closes the form (by clicking on the [x] button) the form still exists in the memory -- it only gets hidden. In MDI applications, closing an MDI child form only minimizes it.
The OnClose event provides an Action parameter (of the TCloseAction type) you can use to specify what happens when a user attempts to close the form. Setting this parameter to "caFree" will free the form.

Delphi tips navigator:
» Get the full HTML from the TWebBrowser component
« How to Convert Pixels to Millimeters

Format
mla apa chicago
Your Citation
Gajic, Zarko. "TForm.Create(AOwner)." ThoughtCo, Jan. 29, 2020, thoughtco.com/tform-createaowner-aowner-1057563. Gajic, Zarko. (2020, January 29). TForm.Create(AOwner). Retrieved from https://www.thoughtco.com/tform-createaowner-aowner-1057563 Gajic, Zarko. "TForm.Create(AOwner)." ThoughtCo. https://www.thoughtco.com/tform-createaowner-aowner-1057563 (accessed March 19, 2024).