uses extctrls;
var
Form1: TForm1;
a :array [0..2] of TShape;
i :Integer;
procedure TForm1.FormCreate(Sender: TObject);
begin
for i :=0 to 2do
begin
a :=TShape.Create(self);
a.Parent :=Form1;
a.Left :=i*200;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
for i :=0 to 2do
a.Free;
end;
第一次按button时已经把对象释放掉,你再第二次释放当然会出现错误了。修改如下:
procedure TForm1.Button1Click(Sender: TObject);
begin
for i :=0 to 2do
if assigned(a) then
begin
a.Free;
a:=nil;
end;
end;