VS2010 _DELETE_CRT macro in STL



如何在VS2010的STL实现中关闭"对调试堆的支持"?我写了一个内存跟踪器,它重载new和delete,并在分配之前添加一个跟踪节点。遗憾的是,一些STL对象使用了我的运算符new,然后尝试用下面的宏删除,而不是我的运算符delete。这意味着他们试图free一个malloc没有返回的地址。由于下面的宏,我的代码在调试时崩溃了,但在发布时没有崩溃(哈哈)。

来自<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在任何配置中都会做的一样。

在您的环境中设置_NO_DEBUG_HEAP=1

(顺便说一句,这是一个环境变量,而不是#define

最新更新