最近用DELPHI写一个线程的程序,会不定时出现access violation at address  后面的地址经常不一样,有时候是00000000,有时候是其他的,请大虾们帮忙看下下面的程序有什么问题//-------------定义线程-------------------------------
 type
  TMyThread = class(TThread)
   protected
   procedure Execute; override;
   end;procedure TMyThread.Execute;
var
tmpList:TStringList;
  index,row:Integer;
beginFreeOnTerminate := True;
 if NewMsgBox.OpenDialog1.Execute then   //获取文件的路径
    begin
      if waitplease = nil then
       waitplease:= Twaitplease.Create(nil);
       waitplease.Show;               //显示进度条的窗体
       tmpList:=TStringList.Create;
       tmpList.LoadFromFile(NewMsgBox.OpenDialog1.FileName);   //把文本文件导入StringList;
       row:=NewMsgBox.PhoneGrid.RowCount;                     //NewMsgBox中StringGrid的行数
       waitplease.Gauge1.Visible := true;                    //设置进度条相关属性
       waitplease.Gauge1.Invalidate;
       waitplease.Gauge1.Progress := 0;
       waitplease.Gauge1.MaxValue := tmpList.Count-1;
      for index:=0 to tmpList.Count-1 do                  //把StringList中的内容循环读近NewMsgBox的StringGrid
        begin
          if CheckPhoneNumber(tmpList[index]) then
            begin
              NewMsgBox.PhoneGrid.Cells[0,row]:=tmpList[index];
              row:=row+1;
              NewMsgBox.PhoneGrid.RowCount:=row;
            end;
            waitplease.Gauge1.Progress:=waitplease.Gauge1.Progress+1;    //进度条+1
        end;
      waitplease.close;                    //关闭进度条
      tmpList.Free;                        //释放STRINGLIST
    end;
end;//----------------调用线程----------------------TMyThread.Create(False);

解决方案 »

  1.   

    如果错误重复出现,那么就是错误,而不是BUG,如果错误显示的是访问地址非法,往往是访问了已经释放了的内存,或者访问尚未申请的内存。你在线程中感觉访问了VCL?最好用消息同步吧
      

  2.   


    你线程是不是有Terminate,FreeOnTerminate := True;
    这个会把线程Free的
    你是不是又有唤醒代码拉
      

  3.   

    type
      TMyThread = class(TThread)
      protected
      procedure Execute; override;
      end;procedure TMyThread.Execute;
    var
    tmpList:TStringList;
      index,row:Integer;
    beginFreeOnTerminate := True;
     if NewMsgBox.OpenDialog1.Execute then //获取文件的路径
      begin
      if waitplease = nil then
      waitplease:= Twaitplease.Create(nil);
      waitplease.Show; //显示进度条的窗体
      tmpList:=TStringList.Create;
      tmpList.LoadFromFile(NewMsgBox.OpenDialog1.FileName); //把文本文件导入StringList;
      row:=NewMsgBox.PhoneGrid.RowCount; //NewMsgBox中StringGrid的行数
      waitplease.Gauge1.Visible := true; //设置进度条相关属性
      waitplease.Gauge1.Invalidate;
      waitplease.Gauge1.Progress := 0;
      waitplease.Gauge1.MaxValue := tmpList.Count-1;
        NewMsgBox.PhoneGrid.RowCount:=tmpList.Count-1;
      for index:=0 to tmpList.Count-1 do //把StringList中的内容循环读近NewMsgBox的StringGrid
      begin
      if CheckPhoneNumber(tmpList[index]) then
      begin
      NewMsgBox.PhoneGrid.Cells[0,row]:=tmpList[index];
      row:=row+1;
      //NewMsgBox.PhoneGrid.RowCount:=row;
      end;
      waitplease.Gauge1.Progress:=waitplease.Gauge1.Progress+1; //进度条+1
      end;
      waitplease.close; //关闭进度条
      tmpList.Free; //释放STRINGLIST
      end;
    end;
    自己弄好了PhoneGrid的行数每次循环增加一行导致出错,后来我在循环前面先把PhoneGrid的行数给定下来就好了。还是谢谢各位!