Delphi的帮助对TList.Clear如是说的: Call Clear to empty the Items array and set the Count to 0. Clear also frees the memory used to store the Items array and sets the Capacity to 0. 很明显,并没有释放指向的对象。 Delphi在Classes中: procedure TList.Clear; begin SetCount(0); SetCapacity(0); end; 其中用了TList.Delete(Index: Integer);如下: procedure TList.Delete(Index: Integer); var Temp: Pointer; begin if (Index < 0) or (Index >= FCount) then Error(@SListIndexError, Index); Temp := Items[Index]; Dec(FCount); if Index < FCount then System.Move(FList^[Index + 1], FList^[Index], (FCount - Index) * SizeOf(Pointer)); //从这里可以明显的看到,只是将指针的资源回收了,指向的对象没有回收啊 if Temp <> nil then Notify(Temp, lnDeleted); end; 是不是这样: for index := 0 to AList.Count - 1 do TSomeObject(AList[Index]).Free; 然后再TList.Clear或者TList.Free呢,应该是这样的吧?
你的先将Item里面的东东Free掉,再Clear
具体去看TList的clear的源码
Call Clear to empty the Items array and set the Count to 0. Clear also frees the memory used to store the Items array and sets the Capacity to 0.
很明显,并没有释放指向的对象。
Delphi在Classes中:
procedure TList.Clear;
begin
SetCount(0);
SetCapacity(0);
end;
其中用了TList.Delete(Index: Integer);如下:
procedure TList.Delete(Index: Integer);
var
Temp: Pointer;
begin
if (Index < 0) or (Index >= FCount) then
Error(@SListIndexError, Index);
Temp := Items[Index];
Dec(FCount);
if Index < FCount then
System.Move(FList^[Index + 1], FList^[Index],
(FCount - Index) * SizeOf(Pointer)); //从这里可以明显的看到,只是将指针的资源回收了,指向的对象没有回收啊
if Temp <> nil then
Notify(Temp, lnDeleted);
end;
是不是这样:
for index := 0 to AList.Count - 1 do
TSomeObject(AList[Index]).Free;
然后再TList.Clear或者TList.Free呢,应该是这样的吧?