C++ Builder > TControl.Parent 属性 > parent 是否释放了 children?
C++ Builder > TControl.Parent property > does the parent free the children?
我的环境:
C++ Builder 10.2 Tokyo on Windows 10 v1809
我有一个关于 TControl.Parent 属性 的问题。
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TPanel *pnlptr = new TPanel(/*AOwner=*/this);
TLabel *lblptr = new TLabel(/*AOwner=*/this);
lblptr->Parent = pnlptr;
// some processing
//delete lblptr;
delete pnlptr;
}
以上代码是否释放了lblptr?
在文档中(虽然是 2009 年的)
TControl.Parent Property
Note: The Parent property declared in TControl is similar to the Owner property declared in TComponent, in that the Parent of a control frees the control just as the Owner of a component frees that Component.
根据注释,我认为当 pnlptr 被释放时,lblptr 也被释放而没有 delete lblptr
。
这样对吗?
Does the above code free the lblptr?
是的,确实如此。当 TPanel
被释放时,它的基础 TWinControl
析构函数会释放仍在 Controls[]
属性 中的任何组件(将 TWinControl
设置为它们的 Parent
):
destructor TWinControl.Destroy;
var
I: Integer;
Instance: TControl;
begin
...
I := ControlCount;
while I <> 0 do
begin
Instance := Controls[I - 1];
Remove(Instance);
Instance.Destroy; // <-- FREED HERE
I := ControlCount;
end;
...
end;
我的环境:
C++ Builder 10.2 Tokyo on Windows 10 v1809
我有一个关于 TControl.Parent 属性 的问题。
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TPanel *pnlptr = new TPanel(/*AOwner=*/this);
TLabel *lblptr = new TLabel(/*AOwner=*/this);
lblptr->Parent = pnlptr;
// some processing
//delete lblptr;
delete pnlptr;
}
以上代码是否释放了lblptr?
在文档中(虽然是 2009 年的) TControl.Parent Property
Note: The Parent property declared in TControl is similar to the Owner property declared in TComponent, in that the Parent of a control frees the control just as the Owner of a component frees that Component.
根据注释,我认为当 pnlptr 被释放时,lblptr 也被释放而没有 delete lblptr
。
这样对吗?
Does the above code free the lblptr?
是的,确实如此。当 TPanel
被释放时,它的基础 TWinControl
析构函数会释放仍在 Controls[]
属性 中的任何组件(将 TWinControl
设置为它们的 Parent
):
destructor TWinControl.Destroy;
var
I: Integer;
Instance: TControl;
begin
...
I := ControlCount;
while I <> 0 do
begin
Instance := Controls[I - 1];
Remove(Instance);
Instance.Destroy; // <-- FREED HERE
I := ControlCount;
end;
...
end;