当前位置:首页 » 软件开发
开发技术指南» 文章正文
    引言: Delphi的接口陷阱 现在我所知的有两大陷阱:i
 

 

 ·数字随机排序    »显示摘要«
    摘要: 假设有一组数字 1~1000,请将它们随机填充在一个数组中要求:1、数组中无重复数字2、整个程式运算很快! william 兄解答var nums: array[1..1000] of integer; i,j,k,temp: integer; begin for i := low(nums) to high(nums) do nums[i] := i; for i :=......
 ·字符串分割扩展 splitex    »显示摘要«
    摘要:   该示例演示了一个字符串扩展splitex例子,文章内含源代码。 ......


Delphi的接口陷阱
上一页   ... function _release: integer; stdcall;

如果不想自己实现这三个方法,你可以使用tcomponent.因为tcomponent已经实现了这三个方法,所以可以从它继承,就不用实现这三个方法了. 【程序编程相关:获取TBitMap图像缓冲区,提高图像处

每次都要实现这三个方法是比较麻烦的,而且更重要的是,我不知道delphi什么时候用以及怎么用这三个方法?所以我也不知道怎么实现这三个方法.j 【推荐阅读:Thread类的创建及使用

function _intfclear(var dest: iinterface): pointer; 【扩展信息:基于阻塞Socket的远程控制类库-TN

这样就可以放心使用了吗?答案是否定的.因为delphi在你把接口变量置为nil时偷偷的(因为很出乎我的意料)调用了_release.

var

  p: pointer;

begin

  result := @dest;

  if dest <> nil then

  begin

    p := pointer(dest);

    pointer(dest) := nil;

    iinterface(p)._release;

  end;

end;

而_release时又做了什么呢?

function tcomponent._release: integer;

begin

  if fvclcomobject = nil then

    result := -1   // -1 indicates no reference counting is taking place

  else

    result := ivclcomobject(fvclcomobject)._release;

end;

不是com对象的话,就什么也没作.我们作的不是com对象,是不是就没有任何问题了呢?答案依然是否定的,考虑如下情况:

obj2 := tc2.create;

try

intf1 := obj2;

intf1.do;

finally

    obj2.free;

    intf1 := nil;

end;

会怎么样呢?会出非法地址访问错误.为什么?上面说过把接口引用设为nil时,会调用_intfclear,而_intfclear又会调用对象的_release,而这时这个对象已经释放了,自然就出非法地址访问错误啦.

有人说多此一举吗,接口引用只是个地址,没必要手动设为nil.

obj2 := tc2.create;

try

intf1 := obj2;


...   下一页
 ·dlphi7中使用system单元的问题    »显示摘要«
    摘要: 在delphi7中如果使用了getmodulename这样的windows函数,如果在help中查是在system单元中,但是在uses中加入system后会出现“identifier redelcared”的错误. 把system改为windows后,编译输出正常. ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE