1. 根據(jù) Delphi 指令參考手冊中
說明:
而參數(shù)為 nil 時則傳回 False 。
技巧:
2.
這個問題要從內(nèi)存方面來解釋
當(dāng)我們無法預(yù)測使用者會如何操爆他的計算機,
Description Use Assigned to determine whether the pointer or procedure referenced by P is nil. P must be a variable reference of a pointer or procedural type. Assigned(P) corresponds to the test P<> nil for a pointer variable, and @P <> nil for a procedural variable. Assigned returns False if P is nil, True otherwise. 檢查指針指向的參考變量或過程是否為 nil 每次我通常的處理方法都是: if assigned(frm) then frm.close;?? 但是當(dāng)下次調(diào)用時就會出錯。為什么呢,直到咋天我才知道原因 frm.close;frm.free; 只是指定這塊內(nèi)存可以重寫,并未釋放為 NIL 因此當(dāng)下次調(diào)用時即使 frm.free 已經(jīng) 執(zhí)行過 assigned(frm) 仍為 TRUE ,再次釋放 frm.Close 或者 frm.free 肯定會報錯;應(yīng)為 frm.Close 或 frm.free 是釋放 對象指針 frm 指向的內(nèi)存空間,在上次已經(jīng)釋放調(diào)了,但是 frm 本身并沒有 初始化為 nil , 相反它還是指向被釋放的內(nèi)存地址;東西已經(jīng)沒有了,沒有地東西去釋放,不報錯錯才怪。 正確的處理方法:
if assigned(frm) then 或 :
if assigned(frm) then
// 可以測試一些就能真正理解 FreeAndNil 和 Assigned 函數(shù)地使用方法了; procedure FreeAndNil(var Obj); Description Use FreeAndNil to ensure that a variable is nil after you free the object it references. Pass any variable that represents an object as the Obj parameter. var P: Pointer;
begin |
關(guān)于delphi Assigned
2009-02-24 08:09
只有注冊用戶登錄后才能發(fā)表評論。 | ||
【推薦】100%開源!大型工業(yè)跨平臺軟件C++源碼提供,建模,組態(tài)!
![]() |
||
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
|
||
|