T
Toysun
Unregistered / Unconfirmed
GUEST, unregistred user!
各位大哥帮帮看看,我把string写入流,用pchar读出来,释放pchar出错
代码如下:
procedure aa(b: string);
begin
ShowMessage(b);
end;
//这里写的很杂,因为是我的程序数据流的简写
procedure TForm1.Button1Click(Sender: TObject);
var
FSendBuffer: TMemoryStream;
aChar;
b,s:String;
begin
FSendBuffer:=TMemoryStream.Create;
b:=Edit1.Text;
//写入流
FSendBuffer.Clear;
FSendBuffer.Write(b, Length(b));
//pchar分配内存
GetMem(a,FSendBuffer.size);
//读出内容
FSendBuffer.Seek(0,soFromBeginning);
FSendBuffer.Read(a,FSendBuffer.Size);
//转换成string
s:=a;
aa(s);
//pchar释放内存
FreeMem(a,FSendBuffer.Size)
//此处出错'Invalid pointer operation',
//a:=nil也一样
//注释掉出不可预知错
FSendBuffer.Free;
end;
代码如下:
procedure aa(b: string);
begin
ShowMessage(b);
end;
//这里写的很杂,因为是我的程序数据流的简写
procedure TForm1.Button1Click(Sender: TObject);
var
FSendBuffer: TMemoryStream;
aChar;
b,s:String;
begin
FSendBuffer:=TMemoryStream.Create;
b:=Edit1.Text;
//写入流
FSendBuffer.Clear;
FSendBuffer.Write(b, Length(b));
//pchar分配内存
GetMem(a,FSendBuffer.size);
//读出内容
FSendBuffer.Seek(0,soFromBeginning);
FSendBuffer.Read(a,FSendBuffer.Size);
//转换成string
s:=a;
aa(s);
//pchar释放内存
FreeMem(a,FSendBuffer.Size)
//此处出错'Invalid pointer operation',
//a:=nil也一样
//注释掉出不可预知错
FSendBuffer.Free;
end;