Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 使用tmathcollection/TMatch遇到内存泄漏_Delphi - Fatal编程技术网

Delphi 使用tmathcollection/TMatch遇到内存泄漏

Delphi 使用tmathcollection/TMatch遇到内存泄漏,delphi,Delphi,我在使用Delphi正则表达式记录时遇到了一个问题。这是我的问题代码: function CrawlThread.CrawlLinks: bool; var Matches: TMatchCollection; Match: TMatch; i: integer; begin Matches:= TRegex.Matches(code, frmCrawler.Edit2.Text); if Matches.Count > 0 then begin

我在使用Delphi正则表达式记录时遇到了一个问题。这是我的问题代码:

function CrawlThread.CrawlLinks: bool;
var
  Matches: TMatchCollection;
  Match: TMatch;
  i: integer;
begin
Matches:= TRegex.Matches(code, frmCrawler.Edit2.Text);
      if Matches.Count > 0 then
      begin
        i:= 0;
        for Match in Matches do
        begin
          SetLength(CrawledLinks, i + 1);
          if (POS('https://', Match.Value) = 0) then
            CrawledLinks[i]:= 'http://' + Match.Value
          else
            CrawledLinks[i]:= Match.Value;
          inc(i);
        end;
        Result:= true;
      end;
      Matches:= TRegex.Matches(code, frmCrawler.Edit3.Text);
      if Matches.Count > 0 then
      begin
        i:= 0;
        for Match in Matches do
        begin
          SetLength(FollowLinks, i + 1);
          if (POS('https://', Match.Value) = 0) then
            FollowLinks[i]:= 'http://' + Match.Value
          else
            FollowLinks[i]:= Match.Value;
          inc(i);
        end;
        Result:= true;
      end;
这段代码在线程中被多次调用,如果我对它进行注释,我的内存使用量会达到26MB,而且不会增长。。。当我使用它时,我会从50MB开始(这不是问题),但它会以每分钟1MB的速度增长(在1分钟内,这个代码会被调用数百次)

使用ReportMemoryLeaksOnShutdown:=true;我得到这个输出:

当注释或使用代码时几乎是一样的,所以我不相信它解释了使用代码时每分钟1MB的情况。当然,Unicoding泄漏困扰着我,但是当我在不使用代码的情况下得到它们时,我不认为它们是问题所在。 你知道为什么代码会占用这么多内存吗

  • 我不认为显示的任何代码是泄漏的,因为和都是纯记录

  • 我见过由于字符串的分配而产生的类似的内存积累。但它必须在一段时间后稳定下来,除非它们被添加到
    TStringList
    中而从未清洗过

  • 这让我想到了下一个问题:消息框中说的是永远不会释放的2x
    TStringList
    。您是否尝试过在项目中搜索所有
    TStringList.Create
    ,并确保存在匹配的
    TStringList.Free
    ? 同样,对于
    TCriticalSection
    TIdHashMessageDigest5

  • 可以肯定的是:在上面的代码中,它似乎是线程类中的一个方法?如果是这样,将导致在VCL线程中引用组件
    frmCrawler.Edit2.Text
    frmCrawler.Edit3.Text
    时出错


  • 此代码不会产生这些泄漏。请打个电话,然后再问一次。是的,我知道用那个代码很难理解。但就像我说的,当我被评论时,我并不是每一分钟都在消耗巨大的内存。。。你确定这个代码不会产生这些泄漏?为什么报告MemoryLeaksonShutdown在使用/不使用该代码时没有显示更多泄漏?可能是TRegex使用巨大内存(即使没有泄漏)的常见情况?请进行修改。或者,如果您想自己解决问题,请获取FastMM并调试泄漏分配堆栈跟踪。可能您需要销毁对象匹配。火柴,免费