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.

169 lines
3.1 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. // tracking.cpp
  6. //
  7. // Purpose:
  8. //
  9. //***************************************************************************
  10. #include "precomp.h"
  11. #include <stdio.h>
  12. #include <time.h>
  13. #define MAX_ELEMENTS 12000
  14. int Trace(const char *fmt, ...);
  15. void __declspec( dllexport ) _SysFreeString(
  16. BSTR bstr,
  17. char *File,
  18. int nLine
  19. )
  20. {
  21. Trace(" ## SysFreeString on 0x%X [%s, %d]\n", bstr, File, nLine);
  22. }
  23. BSTR __declspec( dllexport ) _SysAllocString(
  24. const OLECHAR FAR* sz,
  25. char *File,
  26. int nLine
  27. )
  28. {
  29. BSTR Res = SysAllocString(sz);
  30. Trace("SysAllocString on 0x%X (%S) [%s, %d]\n", Res, sz, File, nLine);
  31. return Res;
  32. }
  33. HRESULT __declspec( dllexport ) DetectLeaks()
  34. {
  35. return 0;
  36. }
  37. void __declspec( dllexport ) _VariantInit(
  38. VARIANTARG FAR* pvarg,
  39. char *File,
  40. int nLine
  41. )
  42. {
  43. Trace("VariantInit on 0x%X [%s, %d]\n", pvarg, File, nLine);
  44. VariantInit(pvarg);
  45. }
  46. HRESULT __declspec( dllexport ) _VariantClear(
  47. VARIANTARG FAR* pvarg,
  48. char *File,
  49. int nLine
  50. )
  51. {
  52. HRESULT hRes;
  53. char *p = 0;
  54. switch (V_VT(pvarg))
  55. {
  56. case VT_I4: p = "VT_I4"; break;
  57. case VT_I2: p = "VT_I2"; break;
  58. case VT_BSTR: p = "VT_BSTR"; break;
  59. case VT_UI1: p = "VT_UI1"; break;
  60. case VT_R8: p = "VT_R8"; break;
  61. default: "<VT_?>";
  62. }
  63. Trace(" ## VariantClear on 0x%X (%s)", pvarg, p);
  64. if (V_VT(pvarg) == VT_BSTR)
  65. Trace(" contains BSTR at address 0x%X (%S)", V_BSTR(pvarg), V_BSTR(pvarg));
  66. Trace(" [%s, %d]\n", File, nLine);
  67. hRes = VariantClear(pvarg);
  68. return hRes;
  69. }
  70. HRESULT __declspec( dllexport ) _VariantCopy(
  71. VARIANTARG FAR* pvargDest,
  72. const VARIANTARG FAR* pvargSrc,
  73. char *File,
  74. int nLine
  75. )
  76. {
  77. Trace("VariantCopy Dest=0x%X Src=0x%X [%s, %d]\n", pvargDest, pvargSrc,
  78. File, nLine);
  79. HRESULT hRes = VariantCopy(pvargDest, (VARIANT *) pvargSrc);
  80. return hRes;
  81. }
  82. CRITICAL_SECTION cs2;
  83. int Trace(const char *fmt, ...)
  84. {
  85. static BOOL bFirstCall = TRUE;
  86. if (bFirstCall)
  87. {
  88. InitializeCriticalSection(&cs2);
  89. bFirstCall = FALSE;
  90. }
  91. EnterCriticalSection(&cs2);
  92. char *buffer = new char[2048];
  93. va_list argptr;
  94. int cnt;
  95. va_start(argptr, fmt);
  96. cnt = vsprintf(buffer, fmt, argptr);
  97. va_end(argptr);
  98. int iRetries = 0;
  99. // Get time.
  100. // =========
  101. char timebuf[64];
  102. time_t now = time(0);
  103. struct tm *local = localtime(&now);
  104. strcpy(timebuf, asctime(local));
  105. timebuf[strlen(timebuf) - 1] = 0; // O
  106. for (;;)
  107. {
  108. FILE *fp = fopen("C:\\OAMEM.LOG", "at");
  109. if (!fp && iRetries++ < 5)
  110. {
  111. Sleep(500);
  112. continue;
  113. }
  114. if (!fp)
  115. break;
  116. if (fp)
  117. {
  118. fprintf(fp, "%s", buffer);
  119. fclose(fp);
  120. break;
  121. }
  122. }
  123. delete buffer;
  124. LeaveCriticalSection(&cs2);
  125. return cnt;
  126. }