Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does FreeNotification work?

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?

like image 646
EProgrammerNotFound Avatar asked Sep 30 '22 23:09

EProgrammerNotFound


1 Answers

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.

like image 158
Mason Wheeler Avatar answered Dec 07 '22 13:12

Mason Wheeler