Delphi 使用tmathcollection/TMatch遇到内存泄漏
我在使用Delphi正则表达式记录时遇到了一个问题。这是我的问题代码: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
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
中而从未清洗过TStringList
。您是否尝试过在项目中搜索所有TStringList.Create
,并确保存在匹配的TStringList.Free
?
同样,对于TCriticalSection
和TIdHashMessageDigest5
frmCrawler.Edit2.Text
和frmCrawler.Edit3.Text
时出错此代码不会产生这些泄漏。请打个电话,然后再问一次。是的,我知道用那个代码很难理解。但就像我说的,当我被评论时,我并不是每一分钟都在消耗巨大的内存。。。你确定这个代码不会产生这些泄漏?为什么报告MemoryLeaksonShutdown在使用/不使用该代码时没有显示更多泄漏?可能是TRegex使用巨大内存(即使没有泄漏)的常见情况?请进行修改。或者,如果您想自己解决问题,请获取FastMM并调试泄漏分配堆栈跟踪。可能您需要销毁对象匹配。火柴,免费