为什么 Delphi 不清楚会增加接口列表中最后一个元素的引用计数?

Why does Delphi unclear increases the refcount of the last element in the list of interfaces?

我使用 Delphi 2007,它几乎完全让我对接口引用计数感到震惊。这个小代码块显示了问题:

program intf;

{$APPTYPE CONSOLE}

uses
  Classes;

type
  IMyIntf = interface(IInterface)
  ['{3DE76B13-1F8D-4BCE-914E-7E3B7FB0FA5A}']
    function GetSelf: TObject;
  end;

  TMyObj = class(TInterfacedObject, IMyIntf)
  private
    FI: Integer;
  public
    constructor Create(i: Integer);
    function GetSelf: TObject;
    property I: Integer read FI;
  end;

var
  i, j: Integer;
  il: TInterfaceList;
  ii: IInterface;
  MyObj: TMyObj;
  IMyObj: IMyIntf;

constructor TMyObj.Create(i: Integer);
begin
  inherited Create;
  FI := i;
end;

function TMyObj.GetSelf: TObject;
begin
  Result := Self;
end;

begin
  // create list of interfaced objects and populate it
  il := TInterfaceList.Create;
  for i := 1 to 3 do
    il.Add(TMyObj.Create(i));

  for j := 1 to 2 do begin
    writeln('start loop #', j);
    i := 1;
    for ii in il do begin
      if ii.QueryInterface(IMyIntf, IMyObj) <> 0 then
        halt(1);
      MyObj := TMyObj(IMyObj.GetSelf);
      // release unnecessary IMyIntf, good housekeeping!
      IMyObj := nil;
      writeln('object #', i, ': ', MyObj.I, ', refcount: ', MyObj.RefCount);
      Inc(i);
    end;
    writeln('end loop #', j);
  end;
end.

一切正常,正如预期的那样,在这个程序 运行 之后,输出如下:

start loop #1
object #1: 1, refcount: 3
object #2: 2, refcount: 3
object #3: 3, refcount: 3
end loop #1
*****
start loop #2
object #1: 1, refcount: 3
object #2: 2, refcount: 3
object #3: 3, refcount: 3
end loop #2
*****

但如果我复制内部 for ii in il 循环并将其粘贴到前一个 之后,完全相同 一个 for ii in il 循环 Delphi 自动引用由于某种原因,TInterfaceList 元素的计数在最后一个元素处中断:

// skipped
  for j := 1 to 2 do begin
    writeln('start first loop #', j);
    i := 1;
    for ii in il do begin
      if ii.QueryInterface(IMyIntf, IMyObj) <> 0 then
        halt(1);
      MyObj := TMyObj(IMyObj.GetSelf);
      // release unnecessary IMyIntf, good housekeeping!
      IMyObj := nil;
      writeln('object #', i, ': ', MyObj.I, ', refcount: ', MyObj.RefCount);
      Inc(i);
    end;

    writeln('end first loop #', j);
    writeln('*****');

    writeln('start second loop #', j);
    i := 1;
    for ii in il do begin
      if ii.QueryInterface(IMyIntf, IMyObj) <> 0 then
        halt(1);
      MyObj := TMyObj(IMyObj.GetSelf);
      // release unnecessary IMyIntf, good housekeeping!
      IMyObj := nil;
      writeln('object #', i, ': ', MyObj.I, ', refcount: ', MyObj.RefCount);
      Inc(i);
    end;
    writeln('end second loop #', j);
  end;
// skipped

这是输出,在第一次循环后注意对象 #3:

start first loop #1
object #1: 1, refcount: 3
object #2: 2, refcount: 3
object #3: 3, refcount: 3
end first loop #1
*****
start second loop #1
object #1: 1, refcount: 3
object #2: 2, refcount: 3
object #3: 3, refcount: 4
end second loop #1
start first loop #2
object #1: 1, refcount: 3
object #2: 2, refcount: 3
object #3: 3, refcount: 4
end first loop #2
*****
start second loop #2
object #1: 1, refcount: 3
object #2: 2, refcount: 3
object #3: 3, refcount: 4
end second loop #2

糟糕!在第一个循环 之后,最后一个元素 RefCount 变为 4 而不是 3 。第一个循环之后的任何附加循环都会中断 Delphi 自动引用计数。如果我再次复制'n'粘贴循环体,最后一个元素 RefCount 变为 5,如果我再次复制'n'粘贴循环体,最后一个元素 RefCount 变为 6 等等——我添加了多少次循环最后一个元素RefCount 变得比应该的大 1。

为什么?要么是我错过了什么,要么是我不清楚,但到底是什么?

提前感谢任何help/suggestion!

用于实现 for/in 循环的枚举器对象持有对列表中最终对象的引用。在枚举期间,枚举器对象持有对当前对象的引用。枚举完成后,对当前对象的引用实际上是对最终对象的引用。

枚举器对象只有在超出范围时才会被销毁。在此程序中,枚举器对象具有全局范围。所以它一直持续到程序结束。每次添加另一个循环时,都会添加另一个枚举器。