2012-01-06 40 views
0

如何在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會做任何配置。

+0

您可以輕鬆地回答自己什麼買他們,如果你通過調試和發佈模式'delete'呼叫加強。在調試中,最終會調用'_free_dbg_nolock'來檢查要刪除的指針(和堆)上的某些內容。在發佈模式中,'operator delete'只會調用'free'。我不知道他們檢查了什麼,爲什麼他們這樣做,但現在不是我們的業務,是嗎?我喜歡那個調試堆。 – Xeo 2012-01-06 22:20:36

+0

@Xeo但是無論使用'_DebugHeapDelete'還是'delete',您都可以獲得調試堆的好處。 '_DebugHeapDelete'完全是'delete',除了繞過'operator delete'並直接調用'free'。這意味着它繞過了用戶的'operator delete'(即使它被分配給用戶的'operator new',這對我來說似乎是一個bug)。 *表示*的意義是什麼? – David 2012-01-06 23:24:59

回答

0

在您的環境中設置_NO_DEBUG_HEAP=1

(這是一個環境變量,而不是一個#define,順便說一句)

+0

這會禁用調試堆,但它不會停止STL在調試中調用'_DebugHeapDelete'而不是'operator delete'。我的問題稍微不同,只是禁用調試堆 - 但我不知道爲什麼VS有STL安裝這些新的/刪除宏的第一位。 – David 2012-01-06 22:18:39