Is there any way to know if an object is created or destroyed inside the class helper?

I have several class helpers for components for creating subcomponents, such as pop-up menus, to access these subcomponents at runtime, I create a Singleton TDictionary.

My question is: how do I know that the owner component is being destroyed to remove the subcomponent from TDictionary?

If it is a specialized component, I add it to the destructor, but I cannot add a constructor and / or destructor in the class helper.

Change - Solution

I created a base object that accepts TObject as parameters, when used, the delete action must be performed manually.

Then I inherited a new class from it, overriding methods to receive only TComponent. Here's what the relevant piece of code looks like:

type     
  TCustomLinkedComponents = class(TCustomLinkedObjects)
  strict private
    type
      TCollector = class(TComponent)
      protected
        procedure Notification(AComponent: TComponent; Operation: TOperation); override;
      end;
  strict private
    FCollector: TCollector;
[..]
  end;

procedure TCustomLinkedComponents.Add(Owner: TComponent; const LinkedName: string; LinkedComponent: TComponent);
begin
  inherited Add(Owner, LinkedName, LinkedComponent);
  FCollector.FreeNotification(LinkedComponent);
end;

procedure TCustomLinkedComponents.TCollector.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited;
  if Operation = opRemove then
    LinkedObjects.Remove(TObject(AComponent));
end;

, .

+5
3

TDictionary TComponent, TDictionary. , TComponent.FreeNotification, .:)

+5

, ,

function IsBeingDestroyed(AComponent : TComponent) : Boolean;
begin
  Result := csDetroying in AComponent.ComponentState;
end;

, FreeNotification - .

FreeNotification .

+2

, . Delphi , / - .

+2

All Articles