procedure TForm2.Button3Click(Sender: TObject); var ListBtn:TList<TButton>; tmpBtn:TButton; i:Integer; begin ListBtn:=TList<TButton>.Create;
//加载100个按钮 for i := 1 to 100 do begin tmpBtn:=TButton.Create(nil); ListBtn.Add(tmpBtn) end;
//list释放100个按钮 for i := 1 to 100 do begin tmpBtn:=ListBtn[0]; FreeAndNil(tmpBtn); //这个不能算彻底清除吧? if ListBtn[0]=nil then begin ShowMessage('ok' ); //根本不执行 end; ListBtn.Delete(0); // 由于ListBt[0] 不是 nil ,所以清除不彻底 end;
procedure TObjectList<T>.Notify(const Value: T; Action: TCollectionNotification); begin inherited; if OwnsObjects and (Action = cnRemoved) then Value.DisposeOf; //这个源码有点复杂,看不懂; end;
procedure TForm2.Button3Click(Sender: TObject); var ListBtn:TList<TButton>; tmpBtn:TButton; i:Integer; begin ListBtn:=TList<TButton>.Create;
//加载100个按钮 for i := 1 to 100 do begin tmpBtn:=TButton.Create(nil); ListBtn.Add(tmpBtn) end;
//list释放100个按钮 for i := 1 to 100 do begin tmpBtn:=ListBtn[0]; FreeAndNil(tmpBtn); //这个不能算彻底清除吧? if ListBtn[0]=nil then begin ShowMessage('ok' ); //根本不执行 end; ListBtn.Delete(0); // 由于ListBt[0] 不是 nil ,所以清除不彻底 end;
var ListBtn:TList<TButton>; tmpBtn:TButton; i:Integer; begin ListBtn:=TList<TButton>.Create; try //加载100个按钮 for i := 1 to 100 do begin tmpBtn:=TButton.Create(nil); ListBtn.Add(tmpBtn) end;
//list释放100个按钮 for i := 1 to 100 do begin ListBtn[i-1].Free; end; finally ListBtn.Free; end; end;
----------------------------------------------
MVCXE中国首个DELPHI MVC WEB框架:https://www.mvcxe.com/
//list释放100个按钮 for i := 0 to ListBtn.Count-1 do begin ListBtn[i].Free; ListBtn[i] := nil;//不做也行,前面已经释放了对象 end;
----------------------------------------------
-
//list释放100个按钮 for i := 0 to ListBtn.Count-1 do begin ListBtn[i].Free; ListBtn[i] := nil;//不做也行,前面已经释放了对象 end;
我稍微修改一下,list本身也要释放的;
for i := 0 to ListBtn.Count-1 do begin ListBtn[0].Free; // ListBtn[0] := nil;//不做也行,前面已经释放了对象 if ListBtn[0]=nil //上面这句不写,不知道会不会是nil,等会试试 showmessage('free ok'); ListBtn.delete(0); //删除第一个,第二个就变成第一个; end;
----------------------------------------------
青云论坛
To qingyun: for i := 1 to 100 do begin tmpBtn:=ListBtn[0]; FreeAndNil(tmpBtn); //这个不能算彻底清除吧? if ListBtn[0]=nil then begin ShowMessage('ok' ); //根本不执行 end; ListBtn.Delete(0); // 由于ListBt[0] 不是 nil ,所以清除不彻底 end;
一般调用模式: for i := 0 to ListBtn.count - 1 do begin tmpBtn:=ListBtn[i]; tmpBtn.free; end; FreeAndNil(ListBtn);
----------------------------------------------
http://www.cnblogs.com/lzl_17948876/
var F:TObjectList<TButton>; begin F:=TObjectList<TButton>.Create(True); F.Add(Button1); F.Free; //同时释放Button1 end;
----------------------------------------------
-
var [weak]B:IInterfaceComponentReference; begin B:=Button1; TButton(B.GetComponent).Caption:='Hello'; Button1.Free; //B同时也变为nil if assigned(B) then ShowMessage('B is nil');
end;
上面程序需要在Delphi 10.1 Berlin及之后版本下才会输出"B is nil"
----------------------------------------------
-
type TInterfaceItem=class [weak]Value:IInterfaceComponentReference; end;
procedure TForm1.FormCreate(Sender: TObject); var BA:array[0..99] of TButton; Item:TInterfaceItem; L:TObjectList<TInterfaceItem>; i: Integer; B:Boolean; begin L:=TObjectList<TInterfaceItem>.Create(True); for i := 0 to 99 do begin BA[i]:=TButton.Create(Self); Item:=TInterfaceItem.Create; Item.Value:=Ba[i]; L.Add(Item) end;
for i := 0 to 99 do BA[i].Free;
B:=False;
for i := 0 to 99 do B:=B or Assigned(L[i].Value);
if not B then ShowMessage('All Buttons in L are nil'); L.Free; end;
上面程序需要在Delphi 10.1 Berlin及之后版本下才会输出"'All Buttons in L are nil"
----------------------------------------------
-