这有点复杂,所以请让我知道这是否没有道理,我们的团队正在编写C ++应用程序,并且我们以前已重载了运算符new.最近,我遇到了这篇文章: www.flipcode/archives/How_To_Find_Memory_Leaks.shtml 关于如何通过我们的内存分配获取调试信息.
Hi this is a little complicated so please let me know if any of this does not make sense, our team is writing a C++ application and we have previously had operator new overloaded. Recently I ran into this article: www.flipcode/archives/How_To_Find_Memory_Leaks.shtml about how to get debug information with our memory allocations.
应用程序内的所有文件#包括一个我们具有编译时平台配置的文件,在该文件中,我添加了以下内容:
All the files within the application #include one file where we have compile-time platform configurations, and within that file I added the following:
#ifdef _DEBUG void* operator new(size_t size, const char *filename, const char *funcname, int line); void* operator new[](size_t size, const char *filename, const char *funcname, int line); #define new new(__FILE__, __FUNCSIG__, __LINE__) #endif由于我们仅链接libcmt.lib进行平台构建,为了使用STL,我删除了旧的操作符new的实现,该实现如下所示:
Since we only link libcmt.lib for our platform build, to use the STL I removed our old implementation of operator new which looked like:
// in a .cpp file: void* operator new(size_t size) { ... }并替换为:
// in the same .cpp file as above... #undef new void* operator new(size_t size, const char *filename, const char *funcname, int line) { ... } #define new new(__FILE__, __FUNCSIG__, __LINE__)这可以很好地进行编译,但是我从libcmt.lib中收到了很多链接器错误:
this works fine for compiling, but I'm getting a bunch of linker errors from libcmt.lib:
例如:libcmt.lib(malloc.obj):错误LNK2001:无法解析的外部符号__imp_HeapAlloc
ex: libcmt.lib(malloc.obj) : error LNK2001: unresolved external symbol __imp_HeapAlloc
重新添加运算符new的旧实现(不带附加参数)使链接器成功链接所有内容.
Adding back the old implementation of operator new (without the additional parameters) allows the linker to link everything successfully.
我的问题:我希望libcmt看到我的宏(#define new new( FILE , FUNCSIG , LINE )),因此何时它链接尝试并链接我定义的版本(带有调试宏).
My question: I want libcmt to see my macro (#define new new(FILE, FUNCSIG, LINE)) and thus when it links try and link the version I defined (with the debug macros).
如何使它正常工作? (我还尝试使用Visual Studio中的属性表来定义宏)
How do I get this to work?? (I also tried using the property sheets within visual studio to define the macro)
推荐答案您无法使其正常运行.如果在包含标准头的任何文件中定义了此宏,则该行为是未定义的.当然,项目的正常演变将导致人们定义类local operator new,或使用new放置,或使用此宏将破坏的多种技术中的任何一种.它与#define while if处于同一级别.即使您不使用标准库,重新定义宏中的关键字也是遇到麻烦的肯定方法.
You can't get it to work. If this macro is defined in any file that includes a standard header, the behavior is undefined. And of course, normal evolution of a project will lead people to define class local operator new, or use placement new, or any one of a number of techniques which this macro will break. It's on about the same level as #define while if. Redefining a keyword in a macro is a sure way of getting into trouble, even if you don't use the standard library.
更多推荐
C ++重载运算符new delete,
发布评论