用StrCopy给一个PChar类型变量赋值,不用它时怎么清楚
用StrCopy给一个PChar类型变量赋值,不用它时如何清楚
var
Str: String;
Len := Length(Str);
if Len > 0 then
begin
GetMem(Text, Len + 1);
ZeroMemory(Text, Len + 1);
StrPCopy(Text, Str);
end;
这样清除可以吗(如下)?如果不可以要用什么方法
if AContent.Text <> nil then
FreeMem(AContent.Text );
------解决方案--------------------
可以释放空间.
如果Text不是临时变量,在每次GetMem前要FreeMem,还有保证最后一位为#0
------解决方案--------------------
上面代码明明没有AContent的,怎么跳出来的,是什么东西?
------解决方案--------------------
完全可不采用你的方法,调一个库函数:SetLength 即可
------解决方案--------------------
这样可以释放!
------解决方案--------------------
吐雪写出我写的代码,如下:
procedure TSocketForm.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
strCommand,strSegmentLen,strParameter:string;
nSegmentLen,nCommand,Len,Counter:integer;
begin
//防止TCP/IP的粘连现像
setLength(strSegmentLen,3);
strCommand:=trim(Socket.ReceiveText);
Counter:=1;
Len:=Length(strCommand);
while (Counter <Len) do
begin
strSegmentLen:= ' ';
strSegmentLen:=copy(strCommand,Counter,3); // 读取帧长度
nSegmentLen:=stringTointeger(strSegmentLen);
if nSegmentLen=-1 then
exit;
NetParameter[IndexofNP].strCommand:=copy(strCommand,Counter+3,6);//命令
strParameter:=copy(strCommand,Counter+9,nSegmentLen-9);//参数
strcopy(NetParameter[IndexofNP].str,pchar(strParameter));
PostMessage(Handle,WM_NETMESSAGE,0,IndexofNP);
Counter:=Counter+nSegmentLen;
IndexofNP:=IndexofNP+1;
if IndexofNP> =245 then
IndexofNP:=0;
end;
end;
------解决方案--------------------
同意 chenzhuo(Jack Chen),释放了内存,指针并不会置为空。
------解决方案--------------------
不要把FreeMem 当成了 FreeAndNil 使,哈哈!
------解决方案--------------------
把一个简单的东西,搞得这么复杂
------解决方案--------------------
野指针吧!
------解决方案--------------------
指针操作GetMem对应freemem,这个预防针不错
------解决方案--------------------
一样的,照你提供的代码改过后应该都没问题的,只是我怕你其他的地方有问题,仔细检查一下吧!
------解决方案--------------------
ld: LzData 是局部变量,不会对他的成员进行初始化,所以
一开始ld.Title就不是nil, 但是不知道Assigned能通过!
帮助里是这样写的:
function Assigned(var P): Boolean;
Description
Use Assigned to determine whether the pointer or procedure referenced by P is nil. P must be a variable reference of a pointer or procedural type. Assigned(P) corresponds to the test P <> nil for a pointer variable, and @P <> nil for a procedural variable.
Assigned returns False if P is nil, True otherwise.
Note: Assigned can 't detect a dangling pointer--that is, one that isn 't nil but no longer points to valid data. For example, in the code example for Assigned, Assigned won 't detect the fact that P isn 't valid.
var
Str: String;
Len := Length(Str);
if Len > 0 then
begin
GetMem(Text, Len + 1);
ZeroMemory(Text, Len + 1);
StrPCopy(Text, Str);
end;
这样清除可以吗(如下)?如果不可以要用什么方法
if AContent.Text <> nil then
FreeMem(AContent.Text );
------解决方案--------------------
可以释放空间.
如果Text不是临时变量,在每次GetMem前要FreeMem,还有保证最后一位为#0
------解决方案--------------------
上面代码明明没有AContent的,怎么跳出来的,是什么东西?
------解决方案--------------------
完全可不采用你的方法,调一个库函数:SetLength 即可
------解决方案--------------------
这样可以释放!
------解决方案--------------------
吐雪写出我写的代码,如下:
procedure TSocketForm.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
strCommand,strSegmentLen,strParameter:string;
nSegmentLen,nCommand,Len,Counter:integer;
begin
//防止TCP/IP的粘连现像
setLength(strSegmentLen,3);
strCommand:=trim(Socket.ReceiveText);
Counter:=1;
Len:=Length(strCommand);
while (Counter <Len) do
begin
strSegmentLen:= ' ';
strSegmentLen:=copy(strCommand,Counter,3); // 读取帧长度
nSegmentLen:=stringTointeger(strSegmentLen);
if nSegmentLen=-1 then
exit;
NetParameter[IndexofNP].strCommand:=copy(strCommand,Counter+3,6);//命令
strParameter:=copy(strCommand,Counter+9,nSegmentLen-9);//参数
strcopy(NetParameter[IndexofNP].str,pchar(strParameter));
PostMessage(Handle,WM_NETMESSAGE,0,IndexofNP);
Counter:=Counter+nSegmentLen;
IndexofNP:=IndexofNP+1;
if IndexofNP> =245 then
IndexofNP:=0;
end;
end;
------解决方案--------------------
同意 chenzhuo(Jack Chen),释放了内存,指针并不会置为空。
------解决方案--------------------
不要把FreeMem 当成了 FreeAndNil 使,哈哈!
------解决方案--------------------
把一个简单的东西,搞得这么复杂
------解决方案--------------------
野指针吧!
------解决方案--------------------
指针操作GetMem对应freemem,这个预防针不错
------解决方案--------------------
一样的,照你提供的代码改过后应该都没问题的,只是我怕你其他的地方有问题,仔细检查一下吧!
------解决方案--------------------
ld: LzData 是局部变量,不会对他的成员进行初始化,所以
一开始ld.Title就不是nil, 但是不知道Assigned能通过!
帮助里是这样写的:
function Assigned(var P): Boolean;
Description
Use Assigned to determine whether the pointer or procedure referenced by P is nil. P must be a variable reference of a pointer or procedural type. Assigned(P) corresponds to the test P <> nil for a pointer variable, and @P <> nil for a procedural variable.
Assigned returns False if P is nil, True otherwise.
Note: Assigned can 't detect a dangling pointer--that is, one that isn 't nil but no longer points to valid data. For example, in the code example for Assigned, Assigned won 't detect the fact that P isn 't valid.