Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.8 KiB

  1. #ifdef DEBUG
  2. #include <dbgmem.h>
  3. //EXTERN_C BOOL g_bUseNewLeakDetection;
  4. BOOL g_bRegistered = FALSE;
  5. HINSTANCE g_hinstDBG;
  6. #define SHELLDBG "ShellDBG.DLL"
  7. STDAPI_(void) _GetProcFromDLL(HMODULE* phmod, LPCSTR pszDLL, FARPROC* ppfn, LPCSTR pszProc);
  8. // the following are in olestuff.cpp
  9. STDAPI_(void) _DebugMemLeak(UINT flag, LPCTSTR pszFile, UINT iLine);
  10. STDAPI_(void) _add_to_memlist(HMODULE hmod, void *pv, UINT nSize, UINT nType, LPCSTR pszFile, const INT_PTR iLine);
  11. STDAPI_(void) _remove_from_memlist(void *pv);
  12. STDAPI_(void) _remove_from_thread_memlist(DWORD dwThreadId, LPVOID pv);
  13. STDAPI_(void) _transfer_to_thread_memlist(DWORD dwThread, LPVOID pv);
  14. STDAPI_(UINT) _mem_thread_message(void);
  15. STDAPI_(void) _received_for_thread_memlist(DWORD dwFlags, void * pData);
  16. STDAPI_(void) _register_intelli_dump(HMODULE hmod, const INTELLILEAKDUMPCBFUNCTIONS *pfns);
  17. STDAPI_(void) MemLeakInit(UINT wFlags, LPCTSTR pszFile, UINT iLine);
  18. STDAPI_(void) MemLeakTerminate(DWORD dwFlags);
  19. // should be in olestuff.cpp of each dll
  20. STDAPI_(void) DumpLeakedMemory(PLEAKMEMINFO pmeminfo);
  21. STDAPI_(LPWSTR) GetLeakSymbolicName(PLEAKMEMINFO pmeminfo, LPWSTR pwszBuf, int cchBuf);
  22. static const INTELLILEAKDUMPCBFUNCTIONS c_ildcbf = {
  23. DumpLeakedMemory,
  24. GetLeakSymbolicName
  25. };
  26. #define DELAY_LOAD_LEAK_VOID(_fn, _ord, _args, _nargs) \
  27. void __stdcall _fn _args \
  28. { \
  29. static void (__stdcall *_pfn##_fn) _args = NULL; \
  30. if (g_bUseNewLeakDetection) \
  31. { \
  32. if (!g_bRegistered) \
  33. { \
  34. register_intelli_dump(HINST_THISDLL, &c_ildcbf); \
  35. g_bRegistered = TRUE; \
  36. } \
  37. _GetProcFromDLL(&g_hinstDBG, SHELLDBG, (FARPROC*)&_pfn##_fn, (LPCSTR)_ord); \
  38. } \
  39. else \
  40. _pfn##_fn = _##_fn; \
  41. if (_pfn##_fn) \
  42. _pfn##_fn _nargs; \
  43. return; \
  44. }
  45. #define DELAY_LOAD_LEAK_FN(_ret, _fn, _ord, _args, _nargs, _err) \
  46. _ret __stdcall _fn _args \
  47. { \
  48. static _ret (__stdcall *_pfn##_fn) _args = NULL; \
  49. if (g_bUseNewLeakDetection) \
  50. { \
  51. if (!g_bRegistered) \
  52. { \
  53. register_intelli_dump(HINST_THISDLL, &c_ildcbf); \
  54. g_bRegistered = TRUE; \
  55. } \
  56. _GetProcFromDLL(&g_hinstDBG, SHELLDBG, (FARPROC*)&_pfn##_fn, (LPCSTR)_ord); \
  57. } \
  58. else \
  59. _pfn##_fn = _##_fn; \
  60. if (_pfn##_fn) \
  61. return _pfn##_fn _nargs; \
  62. return (_ret)_err; \
  63. }
  64. #define _MemLeakInit _DebugMemLeak // for delay load macro to work
  65. DELAY_LOAD_LEAK_VOID(MemLeakInit, 2,
  66. (UINT wFlags, LPCTSTR pszFile, UINT iLine),
  67. (wFlags, pszFile, iLine))
  68. // does nothing for now because MemLeakInit is called to terminate leak detection as well
  69. STDAPI_(void) MemLeakTerminate(DWORD dwFlags)
  70. {
  71. }
  72. DELAY_LOAD_LEAK_VOID(add_to_memlist, 3,
  73. (HMODULE hmod, void *pv, UINT nSize, UINT nType, LPCSTR pszFile, const INT_PTR iLine),
  74. (hmod, pv, nSize, nType, pszFile, iLine))
  75. DELAY_LOAD_LEAK_FN(UINT, mem_thread_message, 4, (void), (), 0)
  76. DELAY_LOAD_LEAK_VOID(received_for_thread_memlist, 5, (DWORD dwFlags, void *pData), (dwFlags, pData))
  77. DELAY_LOAD_LEAK_VOID(remove_from_memlist, 6, (void *pv), (pv))
  78. DELAY_LOAD_LEAK_VOID(remove_from_thread_memlist, 7, (DWORD dwThreadId, LPVOID pv), (dwThreadId, pv))
  79. DELAY_LOAD_LEAK_VOID(transfer_to_thread_memlist, 8, (DWORD dwThreadId, LPVOID pv), (dwThreadId, pv))
  80. //DELAY_LOAD_LEAK_VOID(register_intelli_dump, 9, (HMODULE hmod, const INTELLILEAKDUMPCBFUNCTIONS *pfns), (hmod, pfns))
  81. void register_intelli_dump(HMODULE hmod, const INTELLILEAKDUMPCBFUNCTIONS *pfns)
  82. {
  83. static void (__stdcall *pfnregister_intelli_dump)(HMODULE hmod, const INTELLILEAKDUMPCBFUNCTIONS *pfns) = NULL;
  84. if (!pfnregister_intelli_dump)
  85. {
  86. if (g_bUseNewLeakDetection)
  87. _GetProcFromDLL(&g_hinstDBG, SHELLDBG, (FARPROC*)&pfnregister_intelli_dump, (LPCSTR)9);
  88. else
  89. pfnregister_intelli_dump = _register_intelli_dump;
  90. }
  91. if (pfnregister_intelli_dump)
  92. pfnregister_intelli_dump(hmod, pfns);
  93. }
  94. #endif //DEBUG