错误提示为project project1.exe raise exception class EAccessViolation with 'Access ciolation at address 004051E0 in module 'project1.ext'.read of address 00000018'.Process stoped.use stop or run to continue

解决方案 »

  1.   

    我试了当跟踪到pTest := pa时执行如下操作
    procedure _IntfCopy(var Dest: IInterface; const Source: IInterface);
    {$IFDEF PUREPASCAL}
    var
      P: Pointer;
    begin
      P := Pointer(Dest);
      if Source <> nil then
        Source._AddRef;
      Pointer(Dest) := Pointer(Source);
      if P <> nil then
        IInterface(P)._Release;
    end;
    当点Button2时
    pTest:= pTb;// as ITest;
        pTest.test;
    调用
    _IntfCopy
    pTest保存了先前pTa的值
    if P <> nil then
        IInterface(P)._Release;
    所以相当于pTa._Release,这是引用计数为零被释放
    下次点时会出错
    可以这样
    procedure CallTest(const ATest: ITest);
    begin
      ATest.test;
    end;
    声明为Const不会增加引用计数, 
    然后调用CallTest(pTa), CallTest(pTb), CallTest(pTc);或者
    procedure AddRef;
    begin
      if pTest <> nil then
        pTest._AddRef;
    end;

    每一个赋值之前加上
    AddRef;
    p:= pTb
    增加引用计数
    不过不知道这样好不好
      

  2.   

    主要是pTest为ITest类型
    delphi对接口类型作了引用计数的处理