再问一下,我使用了TList来存储这些指针 最后TList.Clear是否就已经把这些指针内存都释放了呢?而不用用个循环来把这些指针一 个个的Dispose?在clear時要循环一個個的Dispose 我想不必free完后,再clear參考我一個過程 procedure ListClear(List: TList;p: Pointer); var i : integer; begin if Assigned(List) then begin //P-->Dispose for i := 0 to List.count - 1 do begin p := List[i]; try dispose(p); except end; end; List.Clear; end; end;
楼上的大哥,我觉得我还是同意Linux2001大哥的观点。这个是Delphi帮助中说的 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.
我刚才实验了一下, type pi=^integer; var num:integer; i:array[0..9]of pi; j:pi;procedure TForm1.Button1Click(Sender: TObject); begin if num<10 then begin new(i[num]); inc(num); end; end;procedure TForm1.FormCreate(Sender: TObject); begin num:=0; end;procedure TForm1.Button2Click(Sender: TObject); begin if num>0 then begin dec(num); dispose(i[num]); end else dispose(i[0]); end;procedure TForm1.Button3Click(Sender: TObject); begin new(j); end;procedure TForm1.Button4Click(Sender: TObject); begin dispose(j); end;好奇怪,内存时多时少(我是用xp,任务管理器看的)。 不知道为什么?有人可以解释一下吗?
New(pRec)
New(pRec)
New(pRec)
New(pRec)
Dispose(pRec);
最后TList.Clear是否就已经把这些指针内存都释放了呢?而不用用个循环来把这些指针一个个的Dispose?
New(pRec)
New(pRec)
New(pRec)
New(pRec)
Dispose(pRec);回答不是
我new后,都将其加入一个TList,最后在一个个的dispose
但我看帮助说好像不用这么麻烦,直接将TList Clear就行了
对像我做过,指针没有做过。对象(TTest)就是
For i := 0 to List.Count -1 do
try
TTest(List[I]).Free;
finally
List.Free;
end;
Free完后,再使用Tlist.Clear,这样就不会再担心别的地方访问TList的Items里面的指针
最后TList.Clear是否就已经把这些指针内存都释放了呢?而不用用个循环来把这些指针一 个个的Dispose?在clear時要循环一個個的Dispose
我想不必free完后,再clear參考我一個過程
procedure ListClear(List: TList;p: Pointer);
var
i : integer;
begin
if Assigned(List) then
begin
//P-->Dispose
for i := 0 to List.count - 1 do
begin
p := List[i];
try
dispose(p);
except
end;
end; List.Clear;
end;
end;
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.
:)
说白一点
你的 TList 就好像是一个专用的文件夹,
当你在其它地方新建 New(pRec) 一个文件后,
在这个文件夹中放一个快捷方式,如果你将这个 TList 文件夹删除了,
你的 New(pRec) 会被删除吗??????????
type
pi=^integer;
var
num:integer;
i:array[0..9]of pi;
j:pi;procedure TForm1.Button1Click(Sender: TObject);
begin
if num<10 then
begin
new(i[num]);
inc(num);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
num:=0;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if num>0 then
begin
dec(num);
dispose(i[num]);
end
else
dispose(i[0]);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
new(j);
end;procedure TForm1.Button4Click(Sender: TObject);
begin
dispose(j);
end;好奇怪,内存时多时少(我是用xp,任务管理器看的)。
不知道为什么?有人可以解释一下吗?