继 《Delphi 中的自动释放策略》拓展
TComponent分枝也引入了所有权的概念,该概念取自组件库。有两个属性支持所有权:Owner和 Components。每个组件都含有一个Owner属性指定其所有者。
一个组件可能会拥有另外的组件,在这种情况下,所有被包含组件都会出现在该组件的Components属性中。
每个组件的构造器都会包含一个参数指定新组件的所有者。如果该引入的所有者存在,那么这个组件就会自动被添加到所有者的Components属性中。
Components属性除了用来存储所包含组件列表外,还可以提供自动释放其所有的组件。一旦一个组件有了一个所有者,那么当其所有者被释放是,它也会自动释放。
例如:
因为TForm继承自TComponent,所以当一个窗体被销毁时,其所包含的所有组件都会被自动销毁并且释放其所占用的内存(当然,每个组件必须正确地设计其析构器使其能够被自动的清除)。
constructor TComponent.Create(AOwner: TComponent); begin FComponentStyle := [csInheritable]; if AOwner <> nil then AOwner.InsertComponent(Self);//将自己插入到所属者的所属列表中,方便所属者destroy时把自己释放 end; destructor TComponent.Destroy; begin Destroying; RemoveFreeNotifications; DestroyComponents; //释放 if FOwner <> nil then FOwner.RemoveComponent(Self); FObservers.Free; inherited Destro end; procedure TComponent.DestroyComponents; var Instance: TComponent; begin FreeAndNil(FSortedComponents); while FComponents <> nil do //循环释放自己下面所有的Tcomponent begin Instance := FComponents.Last; if (csFreeNotification in Instance.FComponentState) or (FComponentState * [csDesigning, csInline] = [csDesigning, csInline]) then RemoveComponent(Instance) else Remove(Instance); Instance.Destroy; end; end;
其实核心代码就 AOwner.InsertComponent(Self) 这句。举例:
TMyClassA=class(TButton) public procedure KKK(); virtual; end; TMyClassB=class(TMyClassA) public procedure KKK();override; end; TMyClassC=class(TMyClassB) public procedure KKK(); override; end procedure TForm1.FormCreate(Sender: TObject); begin A:=TMyClassA.Create(nil); B:=TMyClassB.Create(nil); C:=TMyClassC.Create(nil); B.InsertComponent(TComponent(A)); //释放B时会连带释放A;以为A已经放到了B里面去管理了 end;