Question

Currently, I am trying to understand Delphi's VCL, specifically, the notification mechanism (which is a great mechanism in my point of view).

When I was studying on this subject, I remembered of the TLabeledEdit, of course I have been using it for long time, but I never had chance to stop and study it's code.

As I understand so far:

When a TComponent is destroyed:

  1. It will notify all its children's components, to include csDestroying in its state.
  2. FreeNotifiers part. I can't understand.
  3. Will iterate the components list and:
    1. Remove each item from the components list
    2. Actually destroy each component instance.

When a child component is being destroyed, it restarts the same process for all of its children components. So, as far as I can tell, this is a chain effect.

What I can't understand is the FreeNotification, what could I possibly do with it?

Let's think about the TLabeledEdit in first place. The relevant part of the notification, in TLabeledEdit's code is an override on the Notification function, with the following code:

  if (AComponent = FEditLabel) and (Operation = opRemove) then
    FEditLabel := nil;

What could have happened if FreeNotification was not used?

In General, what benefits would I have because of this mechanism and what am I not seeing that might eventually make its existence necessary?

Was it helpful?

Solution

What the FreeNotification mechanism does is notifies registered components that this component is being freed. They then use their Notification method to ensure that they don't hold any references to it (which is what your example is doing) so that they don't end up with dangling references to an invalid object.

OTHER TIPS

The point of FreeNotification is to allow you to clean up references to form children in cases where these components close and free themselves. In this example I use a Modal form, the FreeNotification sets the reference to NULL which allows me to detect if the form is still open or already exists if the user creates another one

void __fastcall TForm1::OpenForm(TObject *Sender)
{
    frmGetPrint = new TfrmGetPrint(Application);
    frmGetPrint->FreeNotification( this);
    frmGetPrint->Show();
}

frmGetPrint must free itself when closed:

void __fastcall TfrmSetPrint::FormClose(TObject *Sender, TCloseAction &Action)
{
    Action = caFree;
}

You need to create a function in the parent form referred to by this in the call to FreeNotification( this), in this case Form1

void __fastcall TForm1::Notification(Classes::TComponent* AComponent, 
Classes::TOperation Operation)
{
    if( AComponent == frmGetPrint && Operation == opRemove)
        frmGetPrint = NULL;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top