Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem reading TMemoryStream to a local Variable

Status
Not open for further replies.

44617665

Programmer
Oct 27, 2007
2
GB
Hi

I have a problem reading data from a TMemoryStream object into a local variable, When I call 'ReadBuffer' the call stack is corrupted and my code throws an Exception when I reach the end of the procedure. Reading the Stream into a class member variable works fine but I'd like some help understanding why this fails when using a local variable. As an example of this if I have a form with 2 Edit boxes and a button and the following code under the button click
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  ms : TMemoryStream;
  S : String;
  //S2 : String;
  B : array of Byte;
  I : Integer;
begin
  S := Edit1.Text;
  I := Length(S);
  SetLength(B,I);
  ms := TMemoryStream.Create;
  try

  Move(S[1],B[0],I);

  I := Length(B);
  ms.WriteBuffer(B,I);

  ms.Seek(0, soFromBeginning);
  ms.ReadBuffer(S2,I);
  Edit2.Text := S2;
  finally
    ms.Free;
    SetLength(B,0);
  end;

end;

if S2 above is set to be a class variable then this works as I'd expect and copies the contents from Edit1 to Edit2, however if S2 is a local variable then this fails and throws an exception?

Any suggestions to a better solution than having to declare class member variables?

Many thanks


 
it should be ms.ReadBuffer(S2[1],I);


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks whosrdaddy

Also neeeded to change the ms.writeBuffer to pass in first Element of the byte array but this works.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top