Compiling error when overwriting “new”

▼魔方 西西 提交于 2020-01-06 02:33:08

问题


I'm trying to control mem leaks in my code. In my general header file I've added this code:

    // 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

If I add this code, I get this compilation error:

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>        ]

In addition, In my source code I have this inclusion:

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

Where mem leak control code is into zelCoreLib.h

To those not have VC9.0 this is the code that "fails"

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);
        }

It seems to be part of string and locale facilities. Also, as an additional information, I use Lua and LuaBind libs

Any help will be welcome


回答1:


You have a #define new. new is a keyword, and #defineing it results in undefined behavior if you include any header in the standard library. The standard library almost certainly uses placement new in some places, for example, and your macro will cause any use of placement new to break. It will also cause any class specific new to break—and the library might use these as well. You cannot redefine keywords and expect anything to work.



来源:https://stackoverflow.com/questions/5909062/compiling-error-when-overwriting-new

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!