zoukankan      html  css  js  c++  java
  • 问题-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;

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

  • 相关阅读:
    Word2010如何从指定页设置页码
    十大常见web漏洞及防范
    CSRF攻击与防御
    信息安全常见漏洞类型汇总
    回忆一次校招笔试的题目
    Python操作rabbitmq系列(三):多个接收端消费消息
    Python操作rabbitmq系列(二):多个接收端消费消息
    Python操作rabbitmq系列(一)
    Python操作Redis
    Python连接Redis
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/4653924.html
Copyright © 2011-2022 走看看