是“#define new DEBUG_NEW”吗? 和“#undef THIS_FILE” 等等实际上有必要吗?

发布于 2024-07-13 16:33:02 字数 417 浏览 7 评论 0原文

当您创建新的 MFC 应用程序时,向导会在几乎每个 CPP 文件中创建以下代码块:

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

有时它还会添加以下内容:

#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

如果此代码是多余的,我想从我的 CPP 文件中删除此代码。 我在 VS2008 上使用带有 C++/CLI 的 MFC 应用程序。

从 CPP 中删除此代码后,我尝试在调试中运行,它似乎工作正常。 “new”变量工作正常,没有泄漏,并且 ASSERT 对话框显示正确的文件名并跳转到有问题的行。

谁能告诉我它的作用以及删除它是否安全?

When you create a new MFC application, the wizard creates the following block of code in almost every CPP file:

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

and sometimes it also adds this:

#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

I would like to remove this code from my CPP files if it is redundant. I am using an MFC app with C++/CLI on VS2008.

I have tried running in Debug after deleting this code from a CPP, and it seems to work fine. "new"ing variables work fine, there are no leaks, and ASSERT dialogs show the correct filename and jump to the offending line.

Can anyone tell me what it does and whether it's safe to delete it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦过后 2024-07-20 16:33:02

删除它是完全安全的。 它是一种调试辅助工具; 保留它会在程序退出时出现的任何内存泄漏的输出窗口中的警告中生成更好的详细信息。 如果删除它,您仍然会收到内存泄漏报告,但只是没有有关源代码中发生位置的任何详细信息。

It is perfectly safe to delete this. It's a debugging aid; leaving it in will generate better details in the warnings in the output window of any memory leaks you have when the program exits. If you delete it, you still get the memory leak report, but just without any details about where in your source code they occurred.

ま昔日黯然 2024-07-20 16:33:02

在 Microsoft Visual C++ 2010 上,我可以删除整个代码并仅在标头中放入一个 #define NEW DEBUG_NEW,并且我仍然得到正确的内存泄漏报告,例如

Detected memory leaks!
Dumping objects ->
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {7508} normal block at 0x029B9598, 54 bytes long.
 Data: <                > E4 B8 C9 00 12 00 00 00 12 00 00 00 01 00 00 00 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {7501} normal block at 0x029B94A8, 28 bytes long.
 Data: <                > E4 B8 C9 00 05 00 00 00 05 00 00 00 01 00 00 00 
f:\source\agent\agent\deviceid.cpp(21) : {7500} normal block at 0x029CDFC0, 8 bytes long.
 Data: <        > A8 95 9B 02 B8 94 9B 02 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {6786} normal block at 0x029C0D88, 160 bytes long.
 Data: <        G       > E4 B8 C9 00 19 00 00 00 47 00 00 00 01 00 00 00 
f:\source\agent\sysinfo\sysinfo.cpp(27) : {6733} normal block at 0x029B84D8, 92 bytes long.
 Data: <                > 00 00 00 00 00 10 00 00 00 00 01 00 FF FF FE 7F 
Object dump complete.

On Microsoft Visual C++ 2010, I can remove the whole code and put just one #define NEW DEBUG_NEW in a header, and I still got the right memory leak reports, e.g.

Detected memory leaks!
Dumping objects ->
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {7508} normal block at 0x029B9598, 54 bytes long.
 Data: <                > E4 B8 C9 00 12 00 00 00 12 00 00 00 01 00 00 00 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {7501} normal block at 0x029B94A8, 28 bytes long.
 Data: <                > E4 B8 C9 00 05 00 00 00 05 00 00 00 01 00 00 00 
f:\source\agent\agent\deviceid.cpp(21) : {7500} normal block at 0x029CDFC0, 8 bytes long.
 Data: <        > A8 95 9B 02 B8 94 9B 02 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {6786} normal block at 0x029C0D88, 160 bytes long.
 Data: <        G       > E4 B8 C9 00 19 00 00 00 47 00 00 00 01 00 00 00 
f:\source\agent\sysinfo\sysinfo.cpp(27) : {6733} normal block at 0x029B84D8, 92 bytes long.
 Data: <                > 00 00 00 00 00 10 00 00 00 00 01 00 FF FF FE 7F 
Object dump complete.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文