Question

I am using delphi-xe3 and indy 10 ,unable to resolve this error from 5 hours .everyting is working fine(values displayed as expected ) but when i close form it displays access voilation error.i dont know where is the object that is being freed before creation or is there anything else? or how could i get to the line which is creating this error ?

below is the code of vcl form application.

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.

this is the library file (.pas file)

 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.

the error goes to this thing and points to destroy

 procedure TObject.Free;
    begin
      if Self <> nil then
    {$IFDEF AUTOREFCOUNT}
        __ObjRelease;
    {$ELSE}
  ->      Destroy;
    {$ENDIF}
    end;
Was it helpful?

Solution

Your stomping on your strings...change to

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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top