Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine number of GDI handles and USER objects

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?

like image 900
Gene Avatar asked Nov 02 '22 13:11

Gene


1 Answers

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.

like image 197
jp2code Avatar answered Nov 13 '22 16:11

jp2code