在类变量中使用动态数组来存储在调用类析构函数时需要释放的对象不起作用.
在调用类析构函数之前,数组似乎已经超出了范围并且已经被处理掉了.这是设计的吗?
在XE5中测试的示例:
type
TLeakingObject = class
public
I : Integer;
end;
TTheLeakOwner = class
public
class var OutofScopeArray:array of TLeakingObject;
procedure Add;
class destructor Destroy;
end;
procedure TestThis;
var LeakingTest : TTheLeakOwner;
begin
LeakingTest := TTheLeakOwner.Create;
try
LeakingTest.Add;
finally
LeakingTest.DisposeOf;
end;
end;
{ TTheLeakOwner }
procedure TTheLeakOwner.Add;
begin
setlength(OutofScopeArray, length(OutofScopeArray) + 1);
OutofScopeArray[length(OutofScopeArray) - 1] := TLeakingObject.Create;
end;
class destructor TTheLeakOwner.Destroy;
var I: Integer;
begin
// Length(OutofScopeArray) always = 0, gone out of scope before class destructor ??
for …Run Code Online (Sandbox Code Playgroud)