轨迹句柄创建/删除

发布于 2024-08-11 06:24:33 字数 326 浏览 5 评论 0原文

我有一个大型旧程序,其中有一些相当复杂的图形显示(全部通过标准 API 调用)。该程序似乎工作正常,但我最近在该程序运行时查看了 Windows 任务管理器的“句柄”字段,并注意到句柄的数量逐渐且无情地上升。

我可以使用一些软件或策略来追踪这个恶意手柄的创建吗?

我希望程序创建大量句柄,但我也希望这会达到极限。所以我真正想看到的是代码的哪一部分正在创建最新句柄。

编辑:经过对“Process Explorer”的一些调查,我发现正在爬升的东西是“Handles”而不是“GDI Handles”。所以我想这意味着它与复杂的图形无关。

I have a large old program which has some rather complex graphical displays (all via standard API calls). The program appears to be working fine, but I recently looked at the "handles" field of Windows Task Manager when this program was running and noticed that the number of handles was gradually and relentlessly creeping upwards.

Is there some software or strategy I can employ to trace this rogue handle creation?

I Would expect the program to create a large number of handles, but I would also expect this to reach a limit. So what I really want to see is which part of the code was creating the most recent handles.

EDIT: After some investigation with "Process Explorer" I have discovered that the thing that is creeping up is "Handles" rather then "GDI Handles". So I guess that means its nothing to do with the complex graphics.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只是我以为 2024-08-18 06:24:33

请尝试链接获取建议。问题很复杂,有人写了关于如何解决它的教程。
更新:此处 是另一个可以提供帮助的链接。

Please try this link for advice. Problem is complex and somebody has written tutorial on how to tackle it.
Update: here is one more link that can help.

在巴黎塔顶看东京樱花 2024-08-18 06:24:33

处理此问题的最佳方法是使用 RAII 设计模式

对于您创建的每个句柄,您都将其包装在一个类中。

例如:

class CAutoHandle
{
public:
  CAutoHandle(HANDLE handle) : m_handle(handle)
  {
  }

  ~CAutoHandle()
  {
    CloseHandle(m_handle);
  }

  HANDLE m_handle;
};

@JaredPar 还建议了另一种解决方案 此处,您在其中重新定义 CreateFile 以调用您自己的具有跟踪功能的函数。

The best way to handle this problem is to use the RAII design pattern.

In which for every handle you create you wrap it in a class.

For example:

class CAutoHandle
{
public:
  CAutoHandle(HANDLE handle) : m_handle(handle)
  {
  }

  ~CAutoHandle()
  {
    CloseHandle(m_handle);
  }

  HANDLE m_handle;
};

@JaredPar also suggests another solution here in which you redefine CreateFile to call your own function that has tracking.

耳根太软 2024-08-18 06:24:33

您可以使用内存验证器来查找句柄泄漏。尝试评估版本。

You can use Memory Validator to find the handles leak. Try Eval version.

人生百味 2024-08-18 06:24:33

句柄可以是很多东西,不仅是文件句柄,还可以是 GUI 对象(比如创建图标并使用 DeleteObject() 而不是 DestroyIcon() 删除它)。

MSDN 上有一篇文章介绍了各种技术:
http://msdn.microsoft.com/en-us/magazine/cc301756。 aspx 和一个名为 Leaks.exe 的程序包(假设您仍然可以在 W95 下运行您的代码)

Handles can be a lot of things, not just file handles but also GUI objects (quirks like creating an icon and deleting it with DeleteObject() rather than DestroyIcon()).

There's an MSDN article with various techniques:
http://msdn.microsoft.com/en-us/magazine/cc301756.aspx and a program package called Leaks.exe (assuming that you still can run your code under W95)

挽容 2024-08-18 06:24:33

创建 HANDLE 的其他函数包括 CreateThread、OpenThread、CreateProcess、OpenProcess、CreateMutex、OpenMutex、LoadLibrary 以及可能的 InitializeCriticalSection(不确定该函数)。

我不知道这些是否符合您正在使用的工具中的事件句柄,但它们可能值得检查。

Other functions that create HANDLEs are CreateThread, OpenThread, CreateProcess, OpenProcess, CreateMutex, OpenMutex, LoadLibrary and possibly InitializeCriticalSection (not sure about that one).

I don't know if these qualify as event handles in that tool which you are using but they may be worth checking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文