确定GDI句柄和USER对象的数量 [英] Determine number of GDI handles and USER objects

查看:181
本文介绍了确定GDI句柄和USER对象的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们为Windows Forms UI呈现引擎开发了一个小型测试套件,该套件可以在自动运行测试用例的同时测量性能并检测内存泄漏。现在,我们还要检查手柄是否泄漏。在桌面平台上,我们可以使用以下代码:

We developed a small test suite for our Windows Forms UI rendering engine which allows to measure performance and detect memory leaks while running test cases in an automated manner. Now we would like to check for handle leaks as well. On the desktop platform we can use this code:

[DllImport("User32")]
private extern static int GetGuiResources(IntPtr hProcess, int uiFlags);

using (var process = Process.GetCurrentProcess())
{
  var gdiHandles = GetGuiResources(process.Handle, 0);
  var userHandles = GetGuiResources(process.Handle, 1);
}

该方法似乎在Windows Mobile中不可用。有没有其他方法可以在Windows Mobile / CE上以编程方式确定这些值?

This method doesn't seem to be available in Windows Mobile. Is there another way to determine these values programmatically on Windows Mobile/CE?

推荐答案

这更多是建议而不是答案。

This is more of a suggestion than an answer.

您可能需要将所有处理方法放置在单独的类中(我有一个名为 User32 coreDLL 等)。

You could require that all of your "handle" methods be placed in a separate class (I have a classes called User32, coreDLL, etc).

然后,您可以在每次使用资源时增加一个计数器或将创建的每个新句柄添加到私有管理的列表中,然后再传递回该句柄。

Then, you can either increment a counter every time a resource is used or add each new handle you create to a privately managed list before you pass that handle back.

现在,在项目关闭之前,请确保计数器恢复为零或

Now, before your project closes, make sure your counter is back to zero or all of the handles in your list are closed.

未经测试的代码-目前我没有VS2008。

Untested code - I don't have VS2008 here at the moment.

[DllImport("User32")]
private extern static int GetGuiResources(IntPtr hProcess, int uiFlags);

private static List<IntPtr> m_handles = new List<IntPtr>();

public static IntPtr[] GetGuiResources() {
  using (var process = Process.GetCurrentProcess())
  {
    var gdiHandles = GetGuiResources(process.Handle, 0);
    m_handles.Add(gdiHandles);
    var userHandles = GetGuiResources(process.Handle, 1);
    m_handles.Add(userHandles);
  }
  return new IntPtr[] { m_handles[m_handles.Count - 2], m_handles[m_handles.Count - 1] };
}

public static void Close() {
  for (int i = m_handles.Count - 1; -1 < i; i--) {
     var handle = m_handles[i];
     // release your handle
  }
}

再次,那只是个主意。我以前从未遇到过这个问题,所以我不知道它的运行状况如何,或者是否由于某些我不知道的原因而失败。

Again, that's just an idea. I've never had this issue come up before, so I have no idea how well it works or if it would fail for some "unknown to me" reason.

以及我在逻辑上如何开始项目的方式。

It's just how I would logically go about starting the project.

这篇关于确定GDI句柄和USER对象的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆