_CrtSetAllocHook

Jun 14 2008 4:22 AM

Hi,
I'm trying to find memory leaks by using _CrtSetAllocHook.
As far as I understand I'm supposed to receive notifications whenever a memory chunk is allocated or freed, but I don't get any but the one when I close my application and then when I exit my hooking function - it crashes...
what am I doing wrong?
I'd also like to know how can I monitor a specific  process (other than my C# program), Does  _CrtSetAllocHook gets all the system's allocations? 
 

thanks in advance :)

Here's my code:

private delegate int HookEvent(int allocType, IntPtr wParam, int size, int blockUse, long request, byte[] filename, int line);

[DllImport("MSVCRTD.dll")]

private static extern IntPtr _CrtSetAllocHook(HookEvent hook);

 

private event HookEvent m_hook = null;

public Hooker()

{

m_hook = new HookEvent(Hook);

}

public void Start()

{

IntPtr ptr = _CrtSetAllocHook(m_hook);

}

private int Hook(int allocType, IntPtr wParam, int size, int blockUse, long request, byte[] filename, int line)

{

Console.WriteLine("Event: {0}", ((AllocType)allocType).ToString());

return 1;

}

}