简体   繁体   English

检测MFC应用程序中的内存泄漏

[英]Detecting memory leaks in MFC application

I'm writing an MFC app using Visual 2017 and when the application exits in debug mode, I get this: 我正在使用Visual 2017编写MFC应用程序,当应用程序以调试模式退出时,我得到:

Detected memory leaks! 检测到内存泄漏! Dumping objects -> {74} normal block at 0x00000230E49A7000, 16 bytes long. 转储对象 - > {74}正常块,位于0x00000230E49A7000,长度为16个字节。 Data: <0 0 > 30 00 97 E4 30 02 00 00 00 00 00 00 00 00 00 00 Object dump complete. 数据:<0 0> 30 00 97 E4 30 02 00 00 00 00 00 00 00 00 00 00对象转储完成。

So, in order to know which function is causing the leak, I've added these lines in stdafx.h: 所以,为了知道导致泄漏的函数,我在stdafx.h中添加了这些行:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

And these lines in CWinApp::InitInstance(): CWinApp :: InitInstance()中的这些行:

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetBreakAlloc(74);

Though, it did not work. 虽然,它没有用。 I suspect that the 74th memory allocation number has been made before my code is executed. 我怀疑第74个内存分配号是在我的代码执行之前完成的。 Which method could I overload to be certain to be called first? 我可以通过哪种方法重载以确定首先被调用?

Step into your app to start debugging (that's step, not run, so you'll be stopped in the debugger before anything in your program has run), then set _crtBreakAlloc to the allocation you want to stop at (74). 进入你的应用程序开始调试(这是步骤,而不是运行,所以你将在程序运行之前停止在调试器中),然后将_crtBreakAlloc设置为你想要停止的分配(74)。 Then run and you should get a break on the 74th allocation. 然后跑,你应该在第74次分配中休息。 CRT Debug Heap Details has information on this variable. CRT Debug Heap Details包含有关此变量的信息。

This Microsoft support article also lists instructions for using _crtBreakAlloc in the debugger. 此Microsoft支持文章还列出了在调试器中使用_crtBreakAlloc的说明。

Writing this code 编写这段代码

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

in top of each implementation (.CPP) file, can help you to detect the source of memory leaks. 在每个实现(.CPP)文件的顶部,可以帮助您检测内存泄漏的来源。 See also: How to detect memory leaks in MFC . 另请参阅: 如何检测MFC中的内存泄漏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM