win32_crtdbg.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 2.3.15
  5. * Author : Bruce Liang
  6. * Website : http://www.jessma.org
  7. * Project : https://github.com/ldcsaa
  8. * Blog : http://www.cnblogs.com/ldcsaa
  9. * Wiki : http://www.oschina.net/p/hp-socket
  10. * QQ Group : 75375912
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #pragma once
  25. #if defined _DEBUG && defined _DETECT_MEMORY_LEAK
  26. #ifdef new
  27. #undef new
  28. #endif
  29. #ifdef delete
  30. #undef delete
  31. #endif
  32. #ifndef _CRTDBG_MAP_ALLOC
  33. #define _CRTDBG_MAP_ALLOC
  34. #endif
  35. #include <crtdbg.h>
  36. namespace __dbg_impl
  37. {
  38. class CDebugEnv
  39. {
  40. public:
  41. CDebugEnv()
  42. {
  43. ::_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
  44. ::_CrtMemCheckpoint(&s1);
  45. }
  46. ~CDebugEnv()
  47. {
  48. ::_CrtMemCheckpoint(&s2);
  49. if (::_CrtMemDifference( &s3, &s1, &s2))
  50. {
  51. TRACE("!! Memory stats !!\n");
  52. TRACE("----------------------------------------\n");
  53. ::_CrtMemDumpStatistics(&s3);
  54. TRACE("----------------------------------------\n");
  55. }
  56. }
  57. private:
  58. _CrtMemState s1, s2, s3;
  59. };
  60. static __dbg_impl::CDebugEnv __dbgEnv;
  61. }
  62. #pragma warning(push)
  63. #pragma warning(disable: 4595)
  64. inline void* __cdecl operator new(size_t nSize, const char* lpszFileName, int nLine)
  65. {
  66. // __dbg_impl::CGuard guard;
  67. return ::_malloc_dbg(nSize, _NORMAL_BLOCK, lpszFileName, nLine);
  68. }
  69. inline void* __cdecl operator new[](size_t nSize, const char* lpszFileName, int nLine)
  70. {
  71. return operator new(nSize, lpszFileName, nLine);
  72. }
  73. inline void* __cdecl operator new(size_t nSize)
  74. {
  75. return operator new(nSize, __FILE__, __LINE__);
  76. }
  77. inline void* __cdecl operator new[](size_t nSize)
  78. {
  79. return operator new(nSize, __FILE__, __LINE__);
  80. }
  81. inline void* __cdecl operator new(size_t nSize, const std::nothrow_t&)
  82. {
  83. return operator new(nSize, __FILE__, __LINE__);
  84. }
  85. inline void* __cdecl operator new[](size_t nSize, const std::nothrow_t&)
  86. {
  87. return operator new(nSize, __FILE__, __LINE__);
  88. }
  89. inline void __cdecl operator delete(void* p)
  90. {
  91. // __dbg_impl::CGuard guard;
  92. ::_free_dbg(p, _NORMAL_BLOCK);
  93. }
  94. inline void __cdecl operator delete[](void* p)
  95. {
  96. operator delete(p);
  97. }
  98. inline void __cdecl operator delete(void* p, const char* lpszFileName, int nLine)
  99. {
  100. operator delete(p);
  101. }
  102. inline void __cdecl operator delete[](void* p, const char* lpszFileName, int nLine)
  103. {
  104. operator delete(p);
  105. }
  106. inline void __cdecl operator delete(void *p, const std::nothrow_t&)
  107. {
  108. operator delete(p);
  109. }
  110. inline void __cdecl operator delete[](void *p, const std::nothrow_t&)
  111. {
  112. operator delete(p);
  113. }
  114. #pragma warning(pop)
  115. #define new new(__FILE__, __LINE__)
  116. #endif // _DEBUG && defined _DETECT_MEMORY_LEAK