如何在VS2010的STL實現中關閉「對調試堆的支持」?我編寫了一個內存跟蹤器,它重載了新的和刪除操作,並在分配前添加了一個跟蹤節點。可悲的是一些STL對象使用我的操作符new,然後嘗試使用下面的宏刪除而不是我的操作員刪除。這意味着他們嘗試輸入free
的地址,該地址不會由malloc
返回。由於下面的宏,我的代碼在調試中崩潰,但不在釋放(哈)。STL中的VS2010 _DELETE_CRT宏
從<xdebug>
:
// SUPPORT FOR DEBUG HEAP
#if defined(_DEBUG)
#define _DELETE_CRT(ptr) _STD _DebugHeapDelete(ptr)
template<class _Ty>
void __CLRCALL_OR_CDECL _DebugHeapDelete(_Ty *_Ptr)
{ // delete from the debug CRT heap even if operator delete exists
if (_Ptr != 0)
{ // worth deleting
_Ptr->~_Ty();
// delete as _NORMAL_BLOCK, not _CRT_BLOCK, since we might have
// facets allocated by normal new.
free(_Ptr);
}
}
#else /* defined(_DEBUG) */
#define _DELETE_CRT(ptr) delete (ptr)
僅供參考,準確的崩潰發生在locale operator=
:
locale& operator=(const locale& _Right) _THROW0()
{ // assign a locale
if (_Ptr != _Right._Ptr)
{ // different implementation, point at new one
_DELETE_CRT(_Ptr->_Decref());
_Ptr = _Right._Ptr;
_Ptr->_Incref();
}
return (*this);
}
我甚至不看什麼_DebugHeapDelete
在買他們 - 看起來與什麼operator delete
會做任何配置。
您可以輕鬆地回答自己什麼買他們,如果你通過調試和發佈模式'delete'呼叫加強。在調試中,最終會調用'_free_dbg_nolock'來檢查要刪除的指針(和堆)上的某些內容。在發佈模式中,'operator delete'只會調用'free'。我不知道他們檢查了什麼,爲什麼他們這樣做,但現在不是我們的業務,是嗎?我喜歡那個調試堆。 – Xeo 2012-01-06 22:20:36
@Xeo但是無論使用'_DebugHeapDelete'還是'delete',您都可以獲得調試堆的好處。 '_DebugHeapDelete'完全是'delete',除了繞過'operator delete'並直接調用'free'。這意味着它繞過了用戶的'operator delete'(即使它被分配給用戶的'operator new',這對我來說似乎是一個bug)。 *表示*的意義是什麼? – David 2012-01-06 23:24:59