#undef THIS_FILE的问题

本文探讨了MFC源文件中关于#undef THIS_FILE的用途,主要涉及内存泄漏检测和编译优化。DEBUG_NEW在Debug版本中记录内存分配的文件名和行号,而在Release版本中不包含这些信息,以此平衡性能和调试需求。THIS_FILE宏用于存储当前文件名,减少内存占用。同时,文章提到在某些情况下,直接使用#define DEBUG_NEW new(__FILE__, __LINE__)的疑问,期待更多讨论。" 91662225,8235135,Go语言map与range使用技巧,"['golang', 'map操作', '遍历']

MFC给你生成的cpp文件里面都有这么几行:


   1  #ifdef _DEBUG
   2  #define new DEBUG_NEW
   3  #undef THIS_FILE
   4  static char THIS_FILE[] = __FILE__;
   5   #endif

 

在Visual studio 中通常只用1~3行。

对于这段代码的作用,通常有一下解释:

 

        这个是为了VC检测内存泄露而定义的, 只在 Debug 版本中;

  1.         曾经一个解释说,多次使用__FILE__宏,虽然得到字符串的内容相同,但是可能地址不同,即同一个字符串常量多次用到时占用不同的地址,这样导致需要的内存增加了。为了检测内在泄露, Debug 版本的 new 附加上了调用 new 的文件名与调用所在的行号信息, 这是通过 __FILE__ 和 __LINE__ 来实现的, 这两个属于预定义的内部宏, 而之所以要用 THIS_FILE 来代替 __FILE__, 是为了减少程序大小: 如果
#ifndef H_6B9572DA_A64B_49E6_B234_051480991C89 #define H_6B9572DA_A64B_49E6_B234_051480991C89 #ifndef __cplusplus # error "It's not going to compile without a C++ compiler..." #endif #if defined(BACKWARD_CXX11) #elif defined(BACKWARD_CXX98) #else # if __cplusplus >= 201103L # define BACKWARD_CXX11 # define BACKWARD_ATLEAST_CXX11 # define BACKWARD_ATLEAST_CXX98 # else # define BACKWARD_CXX98 # define BACKWARD_ATLEAST_CXX98 # endif #endif // You can define one of the following (or leave it to the auto-detection): // // #define BACKWARD_SYSTEM_LINUX // - specialization for linux // // #define BACKWARD_SYSTEM_UNKNOWN // - placebo implementation, does nothing. // #if defined(BACKWARD_SYSTEM_LINUX) #elif defined(BACKWARD_SYSTEM_UNKNOWN) #else # if defined(__linux) # define BACKWARD_SYSTEM_LINUX # else # define BACKWARD_SYSTEM_UNKNOWN # endif #endif #include <fstream> #include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include <cstring> #include <cctype> #include <string> #include <new> #include <iomanip> #include <vector> #if defined(BACKWARD_SYSTEM_LINUX) // On linux, backtrace can back-trace or "walk" the stack using the following // libraries: // // #define BACKWARD_HAS_UNWIND 1 // - unwind comes from libgcc, but I saw an equivalent inside clang itself. // - with unwind, the stacktrace is as accurate as it can possibly be, since // this is used by the C++ runtine in gcc/clang for stack unwinding on // exception. // - normally libgcc is already linked to your program by default. // // #define BACKWARD_HAS_BACKTRACE == 1 // - backtrace seems to be a little bit more portable than libunwind, but on // linux, it uses unwind anyway, but abstract away a tiny information that is // sadly really important in order to get perfectly accurate stack traces. // - backtrace is part of the (e)glib library. // // The default is: // #define BACKWARD_HAS_UNWIND == 1 // // Note that only one of the define should be set to 1 at a time. // # if BACKWARD_HAS_UNWIND == 1 # elif BACKWARD_HAS_BACKTRACE == 1 # else # undef BACKWARD_HAS_UNWIND # define BACKWARD_HAS_UNWIND 1 # undef BACKWARD_HAS_BACKTRACE # define BACKWARD_HAS_BACKTRACE 0 # endif // On linux, backward can extract detailed information about a stack trace // using one of the following libraries: // // #define BACKWARD_HAS_DW 1 // - libdw gives you the most juicy details out of your stack traces: // - object filename // - function name // - source filename // - line and column numbers // - source code snippet (assuming the file is accessible) // - variables name and values (if not optimized out) // - You need to link with the lib "dw": // - apt-get install libdw-dev // - g++/clang++ -ldw ... // // #define BACKWARD_HAS_BFD 1 // - With libbfd, you get a fair amount of details: // - object filename // - function name // - source filename // - line numbers // - source code snippet (assuming the file is accessible) // - You need to link with the lib "bfd": // - apt-get install binutils-dev // - g++/clang++ -lbfd ... // // #define BACKWARD_HAS_BACKTRACE_SYMBOL 1 // - backtrace provides minimal details for a stack trace: // - object filename // - function name // - backtrace is part of the (e)glib library. // // The default is: // #define BACKWARD_HAS_BACKTRACE_SYMBOL == 1 // // Note that only one of the define should be set to 1 at a time. // # if BACKWARD_HAS_DW == 1 # elif BACKWARD_HAS_BFD == 1 # elif BACKWARD_HAS_BACKTRACE_SYMBOL == 1 # else # undef BACKWARD_HAS_DW # define BACKWARD_HAS_DW 0 # undef BACKWARD_HAS_BFD # define BACKWARD_HAS_BFD 0 # undef BACKWARD_HAS_BACKTRACE_SYMBOL # define BACKWARD_HAS_BACKTRACE_SYMBOL 1 # endif # if BACKWARD_HAS_UNWIND == 1 # include <unwind.h> // while gcc's unwind.h defines something like that: // extern _Unwind_Ptr _Unwind_GetIP (struct _Unwind_Context *); // extern _Unwind_Ptr _Unwind_GetIPInfo (struct _Unwind_Context *, int *); // // clang's unwind.h defines something like this: // uintptr_t _Unwind_GetIP(struct _Unwind_Context* __context); // // Even if the _Unwind_GetIPInfo can be linked to, it is not declared, worse we // cannot just redeclare it because clang's unwind.h doesn't define _Unwind_Ptr // anyway. // // Luckily we can play on the fact that the guard macros have a different name: #ifdef __CLANG_UNWIND_H // In fact, this function still comes from libgcc (on my different linux boxes, // clang links against libgcc). # include <inttypes.h> extern "C" uintptr_t _Unwind_GetIPInfo(_Unwind_Context*, int*); #endif # endif # include <cxxabi.h> # include <fcntl.h> # include <link.h> # include <sys/stat.h> # include <syscall.h> # include <unistd.h> # include <signal.h> # if BACKWARD_HAS_BFD == 1 // NOTE: defining PACKAGE{,_VERSION} is required before including // bfd.h on some platforms, see also: // https://sourceware.org/bugzilla/show_bug.cgi?id=14243 # ifndef PACKAGE # define PACKAGE # endif # ifndef PACKAGE_VERSION # define PACKAGE_VERSION # endif # include <bfd.h> # ifndef _GNU_SOURCE # define _GNU_SOURCE # include <dlfcn.h> # undef _GNU_SOURCE # else # include <dlfcn.h> # endif # endif # if BACKWARD_HAS_DW == 1 # include <elfutils/libdw.h> # include <elfutils/libdwfl.h> # include <dwarf.h> # endif # if (BACKWARD_HAS_BACKTRACE == 1) || (BACKWARD_HAS_BACKTRACE_SYMBOL == 1) // then we shall rely on backtrace # include <execinfo.h> # endif #endif // defined(BACKWARD_SYSTEM_LINUX) #ifdef BACKWARD_ATLEAST_CXX11 # include <unordered_map> # include <utility> // for std::swap namespace backward { namespace details { template <typename K, typename V> struct hashtable { typedef std::unordered_map<K, V> type; }; using std::move; } // namespace details } // namespace backward #else // NOT BACKWARD_ATLEAST_CXX11 # include <map> namespace backward { namespace details { template <typename K, typename V> struct hashtable { typedef std::map<K, V> type; }; template <typename T> const T& move(const T& v) { return v; } template <typename T> T& move(T& v) { return v; } } // namespace details } // namespace backward #endif // BACKWARD_ATLEAST_CXX11 namespace backward { namespace system_tag { struct linux_tag; // seems that I cannot call that "linux" because the name // is already defined... so I am adding _tag everywhere. struct unknown_tag; #if defined(BACKWARD_SYSTEM_LINUX) typedef linux_tag current_tag; #elif defined(BACKWARD_SYSTEM_UNKNOWN) typedef unknown_tag current_tag; #else # error "May I please get my system defines?" #endif } // namespace system_tag 注释上述代码
最新发布
09-29
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值