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.

81 lines
2.4 KiB

  1. #ifndef _BSTRDEBUG_H
  2. #define _BSTRDEBUG_H
  3. #if _MSC_VER >= 1000
  4. #pragma once
  5. #endif // _MSC_VER >= 1000
  6. // BSTR debugging.
  7. // GIVEAWAY is used when someone else will free it
  8. // TAKEOVER is used when you get one you should be freeing
  9. /*
  10. #ifdef MEM_DBG
  11. #define FANDL __FILE__,__LINE__
  12. #define ALLOC_BSTR(x) g_allocs.track(SysAllocString(x),FANDL)
  13. #define ALLOC_BSTR_LEN(x,y) g_allocs.track(SysAllocStringLen(x,y),FANDL)
  14. #define ALLOC_BSTR_BYTE_LEN(x,y) g_allocs.track(SysAllocStringByteLen(x,y),FANDL)
  15. #define ALLOC_AND_GIVEAWAY_BSTR(x) SysAllocString(x)
  16. #define ALLOC_AND_GIVEAWAY_BSTR_LEN(x,y) SysAllocStringLen(x,y)
  17. #define ALLOC_AND_GIVEAWAY_BSTR_BYTE_LEN(x,y) SysAllocStringByteLen(x,y)
  18. #define GIVEAWAY_BSTR(x) g_allocs.release(x,FANDL)
  19. #define TAKEOVER_BSTR(x) g_allocs.track(x,FANDL)
  20. #define FREE_BSTR(x) { g_allocs.release(x,FANDL); SysFreeString(x);}
  21. #else
  22. */
  23. #define ALLOC_BSTR(x) SysAllocString(x)
  24. #define ALLOC_BSTR_LEN(x,y) SysAllocStringLen(x,y)
  25. #define ALLOC_BSTR_BYTE_LEN(x,y) SysAllocStringByteLen(x,y)
  26. #define ALLOC_AND_GIVEAWAY_BSTR(x) SysAllocString(x)
  27. #define ALLOC_AND_GIVEAWAY_BSTR_LEN(x,y) SysAllocStringLen(x,y)
  28. #define ALLOC_AND_GIVEAWAY_BSTR_BYTE_LEN(x,y) SysAllocStringByteLen(x,y)
  29. #define GIVEAWAY_BSTR(x)
  30. #define TAKEOVER_BSTR(x)
  31. #define FREE_BSTR(x) SysFreeString(x)
  32. //#endif
  33. #include "BstrHash.h"
  34. #pragma warning(disable:4786)
  35. class bstrAllocInfo
  36. {
  37. public:
  38. bstrAllocInfo(LPSTR f, int i, ULONG n) : file(f), line(i), num(n) {};
  39. LPSTR file;
  40. int line;
  41. ULONG num; // Which allocation number is this...
  42. };
  43. typedef CGenericHash<long,bstrAllocInfo*> BSTRLEAKMAP;
  44. class CBstrDebug
  45. {
  46. public:
  47. CBstrDebug();
  48. virtual ~CBstrDebug();
  49. // Usage: track(SysAllocString(L"newbstr"), __FILE__, __LINE__)
  50. // If it leaks, the message will be displayed (as file and line#)
  51. // For more reasonable performance, the message must be a static,
  52. // i.e. we don't free it.
  53. BSTR track(BSTR mem, LPSTR message, int line);
  54. void release(BSTR mem, LPSTR message, int line);
  55. protected:
  56. BSTRLEAKMAP m_allocs;
  57. ULONG m_numAllocs;
  58. };
  59. /*
  60. #ifdef MEM_DBG
  61. extern CBstrDebug g_allocs;
  62. extern ULONG g_breakOnBstrAlloc;
  63. #endif
  64. */
  65. #endif