对象的创建和释放!!,为什么我在释放我的对象时总是显示内存错误??在线等。。内有代码!!type
    Tql = class(Tobject)
      private
      ks: array[0..1]of char;
      ml: array[0..1]of char;
      cd: array[0..1]of char;
      jyh: byte;
      js: array[0..1]of char;
      public
      constructor create;
      destructor free;
      function Read():smallint;
      function ChkSum():smallint;
      function make(hexstr:pchar):smallint;
    end;  constructor Tql.create();
  begin
  inherited;//
  end;  destructor Tql.free();
  begin
  inherited;//
  end;function Tql.Read():smallint;
begin
........
...
end;
function Tql.ChkSum():smallint;
begin
.....
...
end;
function Tql.make(hexstr:pchar):smallint;
begin
....
..
end;////////////////////////////////////
其他单元在引用是如下。
procedure Tfsell.ToolButton3Click(Sender: TObject);
var userQL:Tql
 begin
userQL:=Tql.create;
....
....
...
Tql.free;
fsell.close;
end;为什么上面的free在执行时会出现内存地址错误????
该怎样优化我的这一小段代码?我对对象操作不熟,请多指教,谢谢!!!

解决方案 »

  1.   

    噢,对应该是userQL.free,这个地方是我贴错了!sorry!!!
    程序出错不是这的问题.
    请继续!!!
      

  2.   

    try
      userQL:=Tql.create(nil);
       ....
       ....
       ...
    finally
      userQL.Free;
    end;
      

  3.   

    type
        Tql = class(Tobject)
          private
          ks: array[0..1]of char;
          ml: array[0..1]of char;
          cd: array[0..1]of char;
          jyh: byte;
          js: array[0..1]of char;
          public
          constructor create;
          //destructor free; <---------析构函数??!
          Descturctor Destroy;
          function Read():smallint;
          function ChkSum():smallint;
          function make(hexstr:pchar):smallint;
        end;  constructor Tql.create();
      begin
      inherited;//
      end;  destructor Tql.free();<------------这里也改
      begin
      inherited;//既然不要其它代码,你可以不写析构函数!
      end;function Tql.Read():smallint;
    begin
    ........
    ...
    end;
    function Tql.ChkSum():smallint;
    begin
    .....
    ...
    end;
    function Tql.make(hexstr:pchar):smallint;
    begin
    ....
    ..
    end;
      

  4.   

    对,是析构。这好像不是大问题,我明白你的意思,我不是继承了parent的方法吗!
    如果不写这个析构,能保正对象释放掉吗???
      

  5.   

    析构函是Destroy
    你把free重裁成析构函数当然会出错destructor Destroy;
    这样应该没问题了
      

  6.   

    Free 可以重裁,但不要用destructor 关键字
    用procedure就可以了
      

  7.   

    destructor Tql.free();
    begin
      inherited;//
    end;
    这个里面的代码能不能给出来?
      

  8.   

    对了。你的构造器和析构器都写错了:
          constructor create;
          destructor free;
    改为:
          constructor Create;override;
          destructor Destroy;override;
      

  9.   

    不写构造和析构的声明和函数照样可以调用他们如果在创建时自己没有特殊要求就不必有Create的声明及实现
    如果你的类中没有动态分配内存在释放时就不必有Destroy的声明及实现
      

  10.   

    以上的答案都好象不是问题所在,我根据上面的答复修改了程序,但问题依然存在,具体错误提示为:
    而且该错误是在退出下面的事件时发生的:
    procedure Tfsell.ToolButton3Click(Sender: TObject);
    var userQL:Tql
     begin
    userQL:=Tql.create;
    ....
    ....
    ...
    Tql.free;
    fsell.close;
    end;错误信息为:
    project gasman.exe raised exception class EAccessViolation with message'AccessViolation at Address 00000000.read of address 00000000'.Process stopped.Use Step or Run to continue.我已经调试了两天,我要晕了!!
      

  11.   

    倒数第三句,不是Tql.free
      是userQL.free
      

  12.   

    class ...constructor Create;
    destructor Destroy; override;end;begin
      userQL := Tql.Create;
      ...
      uesrQL.Free;
    end;大体就是这样:重定义(override)Destroy,但是调用的时候要调用 Free
    ================================================================
    人生最大的幸福莫过于顿顿有玉米吃
    (CSDN 论坛助手,挺好使!俺们拨号上网的有福了)
      

  13.   

    blazingfire(烈焰) 兄弟说的对,错误在:
    constructor Create;oerride;
    destructor Destroy;override;
    -----------------
    不过Create一般不带Override的,这样写就对了:
    应为TObject.Create是静态方法,不可以覆盖的。
    constructor Create;
    destructor Destroy;override;
    ...
    destructor Destroy;override;
    begin
      //your Free code here...
      inherited;
    end;
      

  14.   

    检查一下你的FREE(DESTROY)函数,问题出在这个函数内部,如果紧紧是上面的代码,不会出错。
      

  15.   

    procedure Tfsell.ToolButton3Click(Sender: TObject);
    var usrpaswrd: array[0..5] of char;
        resul:smallint;
        bh,gqcs,gql,xsbj,gfbj:string;
        userQL:Tql;
    begin
    strcopy(usrpaswrd,pchar(UF_MMA));
    resul := xiugaiquan(usrpaswrd);
    userQL:= Tql.create;//创建Tql的实例,正常!
    userQL.setbh(pchar(inttostr(strtoint(copy(bh,1,5)))));设置Tql的成员值,正常!
    userQL.setgql(pchar(gql));设置Tql的成员值,正常!
    userQL.setxsbj(pchar(xsbj));设置Tql的成员值,正常!
    userQL.setgfbj(pchar(gfbj));设置Tql的成员值,正常!
    userQL.setmma(pchar(UF_MMA));设置Tql的成员值,正常!
    userQL.make('68a516');将Tql的成员值写到设备上,正常!
    userQL.Read();对设备的数据进行校验,正常!
    userQL.Free;释放实例,正常!
    fsell.close;关闭窗口,正常!
    end;结束,正常!系统停顿1-3秒,弹出错误框!
    ?????????????????????????
      

  16.   

    我参照mywyn的方法注释掉了其他语句只留了
    procedure Tfsell.ToolButton3Click(Sender: TObject);
    var usrpaswrd: array[0..5] of char;
        resul:smallint;
        bh,gqcs,gql,xsbj,gfbj:string;
        userQL:Tql;
    begin
    strcopy(usrpaswrd,pchar(UF_MMA));
    resul := xiugaiquan(usrpaswrd);
    end;结束,正常!系统停顿1-3秒,弹出错误框!问题依然存在!project gasman.exe raised exception class EAccessViolation with message'Access
    Violation at Address 00000000.read of address 00000000'.Process stopped.Use Step
    or Run to continue.下面是相关的系统调用!
    unit ufunctioninterface
    function rsct_4442(icdev: longint;counter:pchar):smallint;stdcall;
    far;external 'MWIC_32.dll'name 'rsct_4442';
    function xiugaiquan(password:pchar):smallint;implementation
    function xiugaiquan(password:pchar):smallint;
      {若卡的错误计数器为0,则为废卡,返回-1;
      输入的密码正确,返回0;
      不正确,返回错误计数器的值+1}
      var
      karpaswrd:array[0..2]of char;
      st,count:smallint;
      begin
      count:=0;
      st := rsct_4442(idev,@count);
      if count >0 then begin
      zh16(karpaswrd,password,6);
      st := csc_4442(idev,3,karpaswrd);
       end;
      if st=0  then   xiugaiquan := 0
      else
      xiugaiquan := -(count+1);
      end;