windows 如何追踪句柄泄漏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7846115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:18:40  来源:igfitidea点击:

How to trace handle leaks?

windowsdelphidelphi-xeresource-leak

提问by jpfollenius

In one of my applications I observe an increasing number of handles. The number is incremented roughly every second without using the application, so there must be a handle leak in some part of the background processing code.

在我的一个应用程序中,我观察到越来越多的句柄。在不使用应用程序的情况下,该数字大约每秒递增,因此后台处理代码的某些部分肯定存在句柄泄漏。

How can I trace such leaks? Are there any tools to help with this? What are the patterns to look for when tracing handle leaks? What are the most common mistakes that cause a handle leak?

我如何追踪此类泄漏?有什么工具可以帮助解决这个问题吗?跟踪句柄泄漏时要寻找哪些模式?导致句柄泄漏的最常见错误是什么?

A bunch of questions, but I hope they are related enough to put them in one question. Thanks for any input in advance!

一堆问题,但我希望它们足够相关,可以将它们放在一个问题中。感谢您提前提供任何意见!

采纳答案by PeterK

I suggest using Process Explorer. Run it as Administrator to make sure you see all the relevant data. There you can observe what kind of handles your process has and which are excessively being created and not free'd. This should help you to narrow down the search (in my case, i had sockets leaking all over the place due to some false assumptions about the behaviour of winsocks.). Perhaps then you can isolate the relevant code and post it here for us to check if there is anything wrong. Good luck!

我建议使用Process Explorer。以管理员身份运行它以确保您看到所有相关数据。在那里你可以观察你的进程有什么样的句柄,哪些是被过度创建而不是被释放的。这应该可以帮助您缩小搜索范围(在我的情况下,由于对 winsocks 行为的一些错误假设,我的套接字到处泄漏。)。或许到时候你可以把相关的代码隔离出来,贴在这里让我们看看有没有什么问题。祝你好运!

回答by PeterK

Some profilers (i.e. AQTime) can profile Windows resource applications. If you're using XE, you have a basic version of AQTime available.

一些分析器(即AQTime)可以分析 Windows 资源应用程序。如果您使用的是 XE,则可以使用 AQTime 的基本版本。

Handle leaks are common if an execution path doesn't close them properly - but a in Windows a handle is a very common item used in very many different situations, which kind of handles is your application leaking?

如果执行路径没有正确关闭它们,句柄泄漏很常见 - 但在 Windows 中,句柄是一个非常常见的项目,用于许多不同的情况,您的应用程序泄漏的是哪种句柄?