5

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);
}

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

1 Answer 1

2

This is more of a suggestion than an answer.

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.

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.

2
  • 1
    As far as I understand your approach, this would require me to rewrite the whole resource handling in our application which is currently handled by Windows Forms (somewhere deep within Pen, Graphics and Bitmap) - just for the sake of testing for resource handle leaks. As such tests are usually performed just before a new version is released, I'll rather observe the applications behavior with the existing tools. Anyhow, thanks for your suggestion - I appreciate the effort.
    – Gene
    Oct 14, 2013 at 7:29
  • Certainly don't do this before you deploy, no. However, this would be a better design to keep in mind for your application. Think future releases. :)
    – user153923
    Oct 14, 2013 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.