问题-Delphi 中使用TStringList后,报out of memory 的解决方法
问题现象:
请一段开发个项目 程序调试全部通过但测试时出现个问题 “out of memory” 在长时间运行时!后来终于解决 :很简单其实就是object.create时对象没有释放。
代码如下:
function SplitString(const Source,ch:string):TStringList; //split 函数
var
temp:String;
i:Integer;
begin
Result:=TStringList.Create; //在这里建立 但无法释放 因为函数返回值是TStringList对象
//如果是空自符串则返回空列表
if Source=''
then exit;
temp:=Source;
i:=pos(ch,Source);
while i<>0 do
begin
Result.add(copy(temp,0,i-1));
Delete(temp,1,i);
i:=pos(ch,temp);
end;
Result.add(temp);
end;
问题原因:
因为新建对象后,未对其释放。
问题解决:
function existsendlog (log:string;sendchangelog:TStringList):boolean; //发送日志中是否存在日志名
var j:integer;
tempchangelogtxt,tempsendlogtxt:TStringList;
resultval:boolean;
begin
resultval:=false;
tempchangelogtxt:=SplitString(log,'|');
for j:=0 to sendchangelog.Count-1 do
begin
tempsendlogtxt:=SplitString(sendchangelog[j],'|');
if tempsendlogtxt[0]= tempchangelogtxt[0] then
begin
resultval:=true;
end;
if tempsendlogtxt<>nil then FreeAndNil(tempsendlogtxt); //没解决前没加此行
end;
if tempchangelogtxt<>nil then FreeAndNil( tempchangelogtxt); //没解决前没加此行
Result:=resultval;
end;
加上上面两条释放问题立刻消失 !问题测试通过的!其它语言开发程序我想应该是一样的!
欢迎关注我,一起进步!扫描下方二维码即可加我


【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步