覆盖“新”时编译错误

时间:2011-05-06 08:57:00

标签: c++ memory-leaks

我正在尝试控制代码中的内存泄漏。在我的通用头文件中,我添加了以下代码:

    // You may need to locate mem leaks
    #define ZEL_CHECK_MEMORY_LEAKS
    #ifdef ZEL_CHECK_MEMORY_LEAKS
        #define _CRTDBG_MAP_ALLOC
        #include <cstdlib>
        #include <crtdbg.h>

        #ifdef _DEBUG
        #ifndef DBG_NEW
        #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
        #define new DBG_NEW
        #endif
        #endif  // _DEBUG

        #define zelInitMemoryCheck() \
            _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF)
    #else
        #define zelInitMemoryCheck()
    #endif //ZEL_CHECK_MEMORY_LEAKS

如果我添加此代码,我会收到此编译错误:

1>c:\archivos de programa\microsoft visual studio 9.0\vc\include\xlocmon(283) : error C2061: syntax error : identifier '_DebugHeapTag_func'
1>        c:\archivos de programa\microsoft visual studio 9.0\vc\include\xlocmon(281) : while compiling class template member function 'size_t std::moneypunct<_Elem,_Intl>::_Getcat(const std::locale::facet **,const std::locale *)'
1>        with
1>        [
1>            _Elem=char,
1>            _Intl=true
1>        ]
1>        c:\archivos de programa\microsoft visual studio 9.0\vc\include\xlocmon(908) : see reference to class template instantiation 'std::moneypunct<_Elem,_Intl>' being compiled
1>        with
1>        [
1>            _Elem=char,
1>            _Intl=true
1>        ]

另外,在我的源代码中,我有这个包含:

#include "core/zelCoreLib.h"
#include <boost/shared_ptr.hpp>

将mem泄漏控制代码放入zelCoreLib.h

对于没有VC9.0的人来说,这是“失败”的代码

static size_t __CLRCALL_OR_CDECL _Getcat(const locale::facet **_Ppf = 0,
        const locale *_Ploc = 0)
        {   // return locale category mask and construct standard facet
        if (_Ppf != 0 && *_Ppf == 0)
            *_Ppf = _NEW_CRT moneypunct<_Elem, _Intl>(
                _Locinfo(_Ploc->name()), 0, true);
        return (_X_MONETARY);
        }

它似乎是字符串和语言环境设施的一部分。另外,作为附加信息,我使用Lua和LuaBind库

欢迎任何帮助

1 个答案:

答案 0 :(得分:3)

你有#define newnew是一个关键字,如果在标准库中包含任何标头,#define会导致未定义的行为。例如,标准库几乎肯定会在某些地方使用新的位置,并且您的宏将导致任何使用新的放置中断。它还会导致任何类特定的new中断 - 并且库也可能会使用它们。您无法重新定义关键字并期望任何工作。