How does FreeNotification work?

delphi, user-interface, vcl

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.

Problem

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: - It will notify all its children's components, to include `csDestroying` in its state. - `FreeNotifiers` part. I can't understand. - Will iterate the `components` list and: - Remove each item from the `components` list - 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?

Original source