问题-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;

加上上面两条释放问题立刻消失 !问题测试通过的!其它语言开发程序我想应该是一样的!

原文地址:https://www.cnblogs.com/FKdelphi/p/4653924.html