2

我将创建的对象传递给另一个对象的构造函数,该对象需要该对象实现的接口。

  ISomeInterface = interface
  ['{840D46BA-B9FB-4273-BF56-AD0BE40AA3F9}']
  end;

  TSomeObject = class(TInterfacedObject, ISomeinterface)
  end;

  TSomeObject2 = class
  private
    FSomeInterface: ISomeinterface;
  public
    constructor Create(SomeObject: ISomeInterface);
  end;

var
Form1: TForm1; // main form
SomeObject: TSomeObject;

constructor TSomeObject2.Create(SomeObject: ISomeInterface);
begin
  FSomeInterface := SomeObject;
end;

// main form creating
procedure TForm1.FormCreate(Sender: TObject);
var SomeObject2: TSomeObject2;
begin
  SomeObject := TSomeObject.Create;
  //  SomeObject2 := TSomeObject2.Create(nil);        // ok
  SomeObject2 := TSomeObject2.Create(SomeObject);     // not ok
  try
  // do some things
  finally
    SomeObject2.Free;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SomeObject.Free; // if passed to a SomeObject2 Constructor - freeing it causing av
end;

在我关闭主窗体后,它给了我一个 AV 和内存泄漏 - 整个主窗体已经泄漏。如果我传递nilTSomeObject构造函数,一切都很好。编译器是否FSomeInterface通过引用计数释放,我不应该尝试SomeObject在 mainForm 中释放destructor?我怎样才能避免它?

4

1 回答 1

7

TSomeObject 从 TInterfacedObject 继承,因此是引用计数的。您的 TSomeObject 实例未进行引用计数,应将其删除或替换为接口变量。

如果您需要在 FormCreate 中创建的 TSomeObject 实例,则应将其分配给 ISomeInterface 类型的变量,以便引用计数也适用于此。

另一种方法是继承自 TInterfacedPersistant 而不是 TInterfacedObject 以避免引用计数。

要解释代码中发生的事情:

procedure TForm1.FormCreate(Sender: TObject);
var SomeObject2: TSomeObject2;
begin
  { Here you create the instance and assign it to a variable holding the instance.
    After this line the reference count of the instance is 0 }
  SomeObject := TSomeObject.Create;
  //  SomeObject2 := TSomeObject2.Create(nil);        // ok
  { Using the instance as a parameter will increase the reference count to 1 }
  SomeObject2 := TSomeObject2.Create(SomeObject);     // not ok
  try
  // do some things
  finally
    { Freeing SomeObject2 also destroys the interface reference FSomeInterface is
      pointing to (which is SomeObject), decreasing the reference count to 0, which
      in turn frees the instance of TSomeObject. }
    SomeObject2.Free;
  end;
  { Now, after SomeObject is freed, the variable points to invalid memory causing the
    AV in FormDestroy. }
end;
于 2013-04-29T07:21:37.013 回答