3

In Delphi XE I always used ReportMemoryLeaksOnShutDown to detect any leaks when exiting my Applications, most my projects were rather small and finding the leaks was usually not too difficult.

In Lazarus there is no such option but I have just found out an option called Heaptrcon, more information on this page: http://wiki.lazarus.freepascal.org/Profiling

In Project Options > Linking I have set the (-gh) flag, now my fears of any leaks have become a reality. I would post code but because there are a lot of different classes and units I have no idea where to begin in fixing these leaks, this is a project far larger than any other I have worked on.

This a screenshot of some of the leaks:

enter image description here

My debugging skills are practically zero, so far I have looked to see every Object or Class I have created, and then checked to see if it has been freed. Because I am working with a lot of TLists and Pointers/Objects etc the leak could be coming from anywhere I guess.

Is there any clues or tips of where to even begin looking? I am looking at the call stack for each block that is size 16, and there is 6 of them, could this mean there are 6 objects not been destroyed correctly?

I am at a loss, where to begin?

Thanks in advance.

4

2 回答 2

3

您真正需要的信息是与每个泄漏对象关联的分配的堆栈跟踪。它们出现在您的屏幕截图中,但显示为地址而不是函数名称。启用调试信息,名称将呈现给您。然后,您可以像在 Delphi 中使用 FastMM 一样跟踪问题。

于 2013-09-04T13:19:34.020 回答
2

正如 David Heffernan 在他早期的评论中提到的那样,应该有一种方法可以将这些地址转换为函数名称。

第一步是确保我启用Generate debugging info for GDB了由于某种原因未启用的功能,然后我得到了这些功能名称。

在跟踪地址后,他们将我带到了我的 TList 对象。现在我正在从事的这个项目实际上是在 Delphi XE 中开始的,但我正在移植到 Lazarus,并且这些错误在 XE 中没有被发现,但是在释放我的 TList 对象时,我这样做了:

var
  P: Pointer;
begin
  for P in MyList do TMyListItem(P).Free;
end;

我没有安装 XE 来测试,但我记得上面没有错误。

我还将对象添加到 Treeview(那些对象是来自的指针MyList),但我没有释放这些对象。但即使在这样做之后,我的泄漏仍然存在。

经过几个小时尝试不同的事情并接近放弃,我意识到我错过了一些简单的事情:

var
  P: Pointer;
begin
  for P in MyList do TMyListItem(P).Free;
  MyList.Free;
end;

我在 Delphi 中错过了那部分,我很肯定它没有抱怨任何泄漏,但在 Lazarus/FPC 中我得到了大量的新泄漏。到目前为止,我所有的泄漏都已经消失了(希望如此),并且不会再回来了。

现在我得到了这个看起来不那么可怕的泄漏报告,因为它报告了zero泄漏:

在此处输入图像描述

于 2013-09-05T07:45:34.097 回答