使用TMatchCollection / TMatch验证内存泄漏

时间:2015-11-21 14:32:14

标签: delphi

我在使用Delphi Regex记录时遇到问题。这是我的问题代码:

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; 我得到此输出: enter image description here

在评论或使用代码时几乎相同,所以我不相信它在使用代码时每分钟解释1MB。当然,UnicodeString泄漏让我感到烦恼,但是即使不使用代码我也能得到它们,但我不认为它们是问题所在。 有没有想过为什么代码消耗这么多内存?

1 个答案:

答案 0 :(得分:1)

  1. 我认为任何显示的代码都没有泄漏,因为TMatchCollectionTMatch是纯粹的记录。

  2. 由于字符串的分配,我看到了类似的内存堆积。但它必须在一段时间后稳定下来,除非它们在没有清理它的情况下被添加到TStringList

  3. 这引导我进入下一个:消息框说明从未释放的2 x TStringList。您是否尝试过搜索所有TStringList.Create项目并确保匹配TStringList.Free? 同样适用于TCriticalSectionTIdHashMessageDigest5

  4. 只是为了确定:在上面的代码中,它似乎是Thread类中的一个方法?如果是这样,则会导致在VCL线程中引用组件frmCrawler.Edit2.TextfrmCrawler.Edit3.Text时出错。