English 中文(简体)
Track handle creation / deletion
原标题:

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.

最佳回答

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.

问题回答

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.

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

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)

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签