Передача ссылки на объект в качестве интерфейса

Я передаю созданный объект конструктору другого объекта, которому нужен интерфейс, который реализует этот объект.

  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 и утечку памяти - утечка всей основной формы. Если я передаю nil конструктору TSomeObject, все в порядке. Компилятор освобождает FSomeInterface путем подсчета ссылок, и мне не следует пытаться освобождать SomeObject в mainForm destructor? Как я могу этого избежать?


person JustMe    schedule 29.04.2013    source источник
comment
Вот что происходит, если вы смешиваете ссылки на объекты и интерфейсы... это может привести к очень неприятным ошибкам.   -  person jpfollenius    schedule 29.04.2013


Ответы (1)


TSomeObject унаследован от TInterfacedObject и, таким образом, имеет подсчет ссылок. Ваш экземпляр TSomeObject не учитывает ссылки и должен быть удален или заменен переменной интерфейса.

Если вам нужен экземпляр TSomeObject, созданный в FormCreate, вы должны присвоить его переменной типа 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;
person Uwe Raabe    schedule 29.04.2013