Delphi中使用Assigned 用法

 关于 Assigned  

1.根據 Delphi 指令參考手冊中

說明:
Assigned 函式在參數不為nil時傳回True,表示指针已经指到某个内存地址,这个内存地址可能是一个对象地首地址,也可能在函数或过程中,声明一个指针变量,没有赋值为nil ,无乱的指向某处,这两个种情况,Assigned(指针变量)都不为nil ,  函数放回True;

而參數為nil時則傳回False。


Assigned 並不是一個真正的函數。

技巧:
用呼叫 Assigned 的方式來取代直接把參數拿來和nil比較,效率會更好。

 

2.這個問題要從記憶體方面來解釋
當你建構一個物件 SomeComponet.Create(Owner);
系統會有一個指標指向這個物件
當你解構一個物件 SomeComponet.Free;
系統會將指標指到的東西殺掉,但是指標還是指在相同的位置
請注意電腦的資源是有限的,
所以可能下一步你的程式要跟系統要資源,
剛才的指標位置,就出現了其他的資料
If Assigned(SomeComponet) then SomeComponet := nil;
先檢查這個物件有沒有在其他地方被設成 nil,
然後再將它設成 nil 。

當我們無法預測使用者會如何操爆他的電腦,
程式員必須留意記憶體的管理。 小弟淺見...


3
function Assigned(var P): Boolean;

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;   但是当下次调用时就会出错。为什么呢,直到咋天我才知道原因

frm.close;frm.free;  只是指定这块内存可以重写,并未释放为NIL 因此当下次调用时即使frm.free已经

执行过assigned(frm)仍为TRUE,再次释放 frm.Close 或者 frm.free 肯定会报错;应为frm.Close或frm.free 是释放 对象指针frm 指向的内存空间,在上次已经释放调了,但是 frm 本身并没有 初始化为 nil ,相反它还是指向被释放的内存地址;东西已经没有了,没有地东西去释放,不报错错才怪。

正确的处理方法:

 if assigned(frm) then
begin
   frm.close;
   frm:=nil;
end;

或:

if assigned(frm) then
begin
  frm.close;
  freeandnil(frm);
end;


 

 

//可以测试一些就能真正理解 FreeAndNil 和 Assigned 函数地使用方法了;

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
  P := nil;
  if Assigned (P) then Writeln ('You won''t see this');
  GetMem(P, 1024); {P valid}
  FreeMem(P, 1024); {P no longer valid and still not nil}
  if Assigned (P) then Writeln ('You''ll see this');
end;


 
视频图像格式DIB和DDB,在MSDN中对它们的解释是:      


    
  device-independent   bitmap   (DIB)    
  An   array   of   bits   combined   with   several   structures   that   specify   the   width   and   height   of   the   bitmap   image   (in   pixels),   the   color   format   of   the   device   where   the   image   was   created,   and   the   resolution   of   the   device   used   to   create   that   image.   A   DIB   generally   has   its   own   color   table,   and   can   therefore   be   displayed   on   a   variety   of   devices.    
   
  device-dependent   bitmap   (DDB)    
  An   array   of   bits   that   can   only   be   used   with   a   particular   display   or   printer.    
   
  DIB由于自带颜色表,理论上说在不同的设备上显示时均可按原来的颜色还原显示,或仿真显示,但是很明显颜色表需要消耗一定的存储空间,并且在每次显示时均要对颜色进行处理,因此速度较慢。DDB由于直接对颜色位平面进行记录,因此显示速度最快,但是在不同的设备上显示时不能保证颜色的还原。  
   
  主要区别就是颜色标,一般来说,在剪贴板中存放的是DDB(比如截屏的时候获得的),在文件中存放的是DIB  

 

 



使用 SysErrorMessage

 

 

在Windows API帮助里我们常看到这几句话。

If the function succeeds, the return value is nonzero. 
If the function fails, the return value is zero. 
To get extended error information, call GetLastError.

Microsoft认为对第一个API返回所有可能的错误代码是效率代下的,所以他们写
了一个独立的API-GetLastError来返回错误代码。但是当你使用GetLastError时
,返回的只是一些数字,你当然不希望发生错误时返回只是一些数字,Microsof
t还是一个独立的API-FormatMessage来把这些数字转换成文字。但是这一个函数
并不是那么好掌握的。
在DELPHI中有一个函数SysErrorMessage把FormatMessage很好的封装起来,
例如
ShowMessage(SysErrorMessage(GetLastError));
简单,方便。
但是你要记住,当发生错误时要马上GetLastError,因为当你调用另一个API时,
它的值马上后改变。

 

 


  freeandnil的说明:

 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值