English 中文(简体)
为什么不叫毁灭?
原标题:Why is Destroy not called?

鉴于以下戴尔菲代码, Foo free d on FormClose ,但 TFoo.Destroy 没有被调用,因此, Bar 不是 Free d,导致内存泄漏?

我是不是漏掉了点什么 或者应该免费打给Foo 某个时候被摧毁了?

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;
最佳回答

您必须在毁灭器的签名上加设反覆标记 。

destructor Destroy(); override;

您应该在毁灭器末端有 < code> 内含 < / code > 。 但是由于您的班级不是来自其它东西, 而是来自“ 诱饵 ”, 我怀疑这无关紧要 。

问题回答

毁灭是虚拟的, 因此,你必须超越它 在你的后代阶级。

TFoo = class
  Bar : TBar;

  constructor Create();
  destructor Destroy(); override; // must add override here
end;

没有重置,你的毁灭器就永远不会被调用, 而基级一则被调用。





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签