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
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
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