1. Home
  2. Computing & Technology
  3. Delphi Programming

A warning on dynamically instantiating components

By Zarko Gajic, About.com

4 of 10

Performance Hit on Construction - 1

When our TTable is created, the Create constructor is called for TTable, and for each ancestor class that overrides the Create constructor, all the way to TComponent.Create. Here's the implementation for the TComponent.Create method:

constructor TComponent.Create(AOwner: TComponent) ;
begin
   FComponentStyle := [csInheritable];
   if AOwner <> nil then AOwner.InsertComponent(Self) ;
end;

Notice that if AOwner is nil, the Create constructor is extremely fast. However, if AOwner is not nil, then AOwner's InsertComponent method is called. InsertComponent looks like this:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TComponent.InsertComponent(AComponent: TComponent) ;
begin
   AComponent.ValidateContainer(Self) ;
   ValidateRename(AComponent, '', AComponent.FName) ;
   Insert(AComponent) ;
   AComponent.SetReference(True) ;
   if csDesigning in ComponentState then
     AComponent.SetDesigning(True) ;
   Notification(AComponent, opInsert) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Note: A test program was created in Delphi to time the dynamic creation of 1000 components with varying initial component counts.

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. Run time VCL creation
  6. A warning on dynamically instantiating Delphi components

©2009 About.com, a part of The New York Times Company.

All rights reserved.