Differences in dynamic creation of an object

4

When I create an object dynamically, for example a form, I do this:

Formulario := TFormulario.Create(nil);  
try  
    Formulario.ShowModal;  
finally  
    Formulario.Free;  
end;  

What's the difference in creating an object by passing the following values to the AOwner parameter in the Create method?

Formulario := TFormulario.Create(Application);  
Formulario := TFormulario.Create(nil);  
Formulario := TFormulario.Create(Self);  
    
asked by anonymous 03.04.2014 / 22:22

1 answer

8

The difference is who owns the window. That is, if the "owner" is deallocated, all forms that own this form as owner are also deallocated.

  • In the first example, form is only deallocated if the entire application is also. This is not strongly recommended because of performance issues because notifications passed to Application take a longer time to process;
  • In the second, form has no owner, ie the programmer should control the deallocation of this form ;
  • In the third, the form belongs to the screen that called it. Recommended for calling-screen modes.
03.04.2014 / 22:25