我正在使用delphi-xe3和indy 10,无法在5小时内解决此错误。每个工作正常(值按预期显示)但是当我关闭表单时显示访问voilation error.i不知道对象在哪里在创作之前被释放还是还有其他什么?或者我怎么能到达创建此错误的行?
下面是vcl表单应用程序的代码。
type
TForm1 = class(TForm)
encode: TButton;
decode: TButton;
Memo1: TMemo;
procedure encodeClick(Sender: TObject);
private
{ Private declarations }
public
obj:pprotocol;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.encodeClick(Sender: TObject);
begin
new(obj);
memo1.Lines.Add('encode click');
obj.username:='xyz';
obj.ex_ip:='36.42.68.411';
obj.i_ip:='192.168.1.1';
obj.ex_port:=6002;
obj.i_port:=6003;
encode_packet(obj);
decode_packet(obj.arr);
dispose(obj);
end;
end.
这是库文件(.pas文件)
type
tprotocol =record
username:string;
ex_ip,i_ip:string;
ex_port,i_port:word;
arr:tbytes;
end;
pprotocol=^tprotocol;
procedure encode_packet(ob:pprotocol);
procedure decode_packet(arr1:tbytes);
implementation
procedure encode_packet(ob:pprotocol);
var
s1:string;
index:word;
size:word;
begin
index:=length(ob.arr);
size:=length(ob.username)*2+length(ob.ex_ip)*2+length(ob.i_ip)+7;
setlength(ob.arr,size+index);
ob.arr[index]:=length(ob.username);
ob.arr[index+1]:=length(ob.ex_ip);
ob.arr[index+2]:=length(ob.i_ip);
move(ob.ex_port,ob.arr[index+3],2);
move(ob.i_port,ob.arr[index+5],2);
index:=index+7;
move(ob.username[1],ob.arr[index],length(ob.username)*2); // Move(Source^, Destination[0], SourceSindexze);
index:=index+length(ob.username)*2;
move(ob.ex_ip[1],ob.arr[index],length(ob.ex_ip)*2);
index:=index+length(ob.ex_ip)*2;
move(ob.i_ip[1],ob.arr[index],length(ob.i_ip)*2);
end;
procedure decode_packet(arr1:tbytes);
var
ind,port,port1:word;
ip,s1,user:string;
begin
ind:=0;
setlength(user,arr1[ind]);
setlength(s1,arr1[ind+1]);
setlength(ip,arr1[ind+2]);
move(arr1[ind+3],port,2);
move(arr1[ind+5],port1,2);
ind:=ind+7;
move(arr1[ind],user[1],length(arr1));
ind:=ind+length(user)*2;
move(arr1[ind],s1[1],length(arr1));
ind:=ind+length(s1)*2;
move(arr1[ind],ip[1],length(arr1));
showmessage(user);
end;
end.
错误归结于此事并指向销毁
procedure TObject.Free;
begin
if Self <> nil then
{$IFDEF AUTOREFCOUNT}
__ObjRelease;
{$ELSE}
-> Destroy;
{$ENDIF}
end;
答案 0 :(得分:2)
你踩到琴弦......改为
move(arr1[ind],user[1],length(user));
ind:=ind+length(user);
move(arr1[ind],s1[1],length(s1));
ind:=ind+length(s1);
move(arr1[ind],ip[1],length(ip));