Using dynamic array in a class variable to store objects that need to be freed when the class destructor is called does not work.
The array seems to have gone out of scope and already disposed of before the class destructor is called. Is this by design ?
Example tested in 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 I := 0 to Length(OutofScopeArray) - 1 do
FreeAndNil(OutofScopeArray[i]);
end;