C ++重载运算符new delete, [英] c++ overload operator new delete,

查看:107
本文介绍了C ++重载运算符new delete,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点复杂,所以请让我知道这是否没有道理,我们的团队正在编写C ++应用程序,并且我们以前已重载了运算符new.最近,我遇到了这篇文章: http://www.flipcode.com/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: http://www.flipcode.com/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,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆