Ste*_*ess 6 delphi memory-leaks destroy
鉴于以下Delphi代码,Foo是Free'开启FormClose,但TFoo.Destroy未被调用 - 因此Bar不是Free'd,导致内存泄漏?
我在这里错过了什么或者不应该Foo.Free在某些时候调用Foo.Destroy?
type
TBar = class
SomeInteger : integer;
end;
TFoo = class
Bar : TBar;
constructor Create();
destructor Destroy();
end;
var
Foo : TFoo;
implementation
constructor TFoo.Create;
begin
Bar := TBar.Create;
Bar.SomeInteger := 2;
end;
destructor TFoo.Destroy;
begin
Bar.Free;
Bar := nil;
showmessage('Destroyed!');
end;
procedure TForm10.FormCreate(Sender: TObject);
begin
Foo := TFoo.Create;
showmessage('Foo created');
end;
procedure TForm10.FormDestroy(Sender: TObject);
begin
Foo.Free;
Foo := nil;
end;
Run Code Online (Sandbox Code Playgroud)
Rob*_*obS 24
您必须使用覆盖标记析构函数的签名.
destructor Destroy(); override;
Run Code Online (Sandbox Code Playgroud)
你应该inherited在析构函数的末尾.但是因为你的类不是从TObject以外的任何东西派生的,所以我怀疑这并不重要.
Nic*_*ges 10
Destroy是虚拟的,因此您必须在后代类中覆盖它.
TFoo = class
Bar : TBar;
constructor Create();
destructor Destroy(); override; // must add override here
end;
Run Code Online (Sandbox Code Playgroud)
如果没有覆盖,则永远不会调用析构函数,而是基类1.
| 归档时间: |
|
| 查看次数: |
6408 次 |
| 最近记录: |