程序退出时错误提示:
project project_1.exe raised exception class EaccessViolation
with message'Access violation at address 004594A3 in module 'project1.exe'.Read of address 00000220'.process stopped.Use Step or Run to contionuel
那里的毛病,请帮我!
程序如下
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StrUtils,IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer,IdSocketHandle,
StdCtrls,Contnrs;
type
TForm1 = class(TForm)
IdUDPServer1: TIdUDPServer;
Memo1: TMemo;
Button1: TButton;
Memo2: TMemo;
Memo3: TMemo;
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
datathread = class(TThread) { 声明线程类 }
private
protected
procedure ddd;
procedure Execute; override;{ 执行线程的方法 }
public
constructor Create(); virtual; { 线程构造器 }
end;
var
Form1: TForm1;
cs:trtlcriticalsection;
gcount:integer;
mcount:integer;
implementation
{$R *.dfm}
procedure datathread.ddd;
begin
form1.Memo1.Lines.Append(inttostr(gcount)+':'+inttostr(mcount));
end;
procedure datathread.Execute;
begin
{ Place thread code here }
try
FreeOnTerminate:=True;
while not Terminated do
begin
entercriticalsection(cs);
if gcount >0 then
dec(gcount);
leavecriticalsection(cs);
synchronize(ddd);
sleep(100);
end;
except
showmessage('threaderror');
end;
end;
constructor datathread.Create();
begin
inherited Create(False);
FreeOnTerminate := True;
end;
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
DataStringStream: TStringStream;
str:string;
begin
DataStringStream := TStringStream.Create('');
try
DataStringStream.CopyFrom(AData, AData.Size);
str:= DataStringStream.DataString;
if leftstr(str,1)='0'
then sleep(2)//memo2.Lines.Add(str)
else if leftstr(str,1)='1'
then sleep(2)//memo3.Lines.Add(str)
else
begin
inc(gcount);
inc(mcount);
end;
finally
DataStringStream.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
gcount:=0;
mcount:=0;
initializecriticalsection(cs);
idudpserver1.Active:=true;
datathread.Create();
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
deletecriticalsection(cs);
end;
end.