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

A warning on dynamically instantiating components

By , About.com Guide

5 of 10

Performance Hit on Construction - 2

A number of methods are called here, but without a doubt the most expensive (in terms of having a negative impact on performance) is the call to Notification. Notification looks like this:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TComponent.Notification(AComponent: TComponent; Operation: TOperation) ;
var
   j: Integer;
begin
   if (FFreeNotifies <> nil) and (Operation = opRemove) then
   begin
     FFreeNotifies.Remove(AComponent) ;
     if FFreeNotifies.Count = 0 then
     begin
       FFreeNotifies.Free;
       FFreeNotifies := nil;
     end;
   end;
   if FComponents <> nil then
     for j := 0 to FComponents.Count - 1 do
       TComponent(FComponents[j]).Notification(AComponent, Operation) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

This time-intensive code here is the last for-loop, which iterates through every single owned component, calling Notification again (it's a iterative call, so every component either owned or indirectly owned by the form (or whatever component was initially passed as the AOwner parameter), will have this method called. Additionally, the Notification method is virtual, so it can be overridden in descendant classes (and often is).

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. 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.