【发布时间】:2015-03-16 14:39:35
【问题描述】:
我使用的是 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'paste 循环体,最后一个元素 RefCount 变为 5,如果我再次复制'n'paste 循环体,最后一个元素 RefCount 变为 6,依此类推——我在最后一个元素中添加了多少次循环RefCount 变得比应有的大 1。
为什么?要么我错过了什么,要么我不清楚什么,但究竟是什么?
感谢任何帮助/建议!
【问题讨论】: