_CrtDumpMemoryLeaks( ) == 1 在第一行代码? [英] _CrtDumpMemoryLeaks( ) == 1 on first line of code?

查看:21
本文介绍了_CrtDumpMemoryLeaks( ) == 1 在第一行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 MFC Visual C++ 项目.据我了解 MSDN_CrtDumpMemoryLeaks() 应该在内存泄漏时返回 TRUE.

I'm working on an MFC Visual C++ project. As I understand from MSDN, _CrtDumpMemoryLeaks() should return TRUE when there are memory leaks.

在注意到它 TRUE 之后,我尝试在代码中找到它变为 TRUE 的第一个点.显然,它从一开始就是正确的.如果我单击 F10(单步执行)开始调试程序,并在监视窗口中输入 _CrtDumpMemoryLeaks(),它会在程序入口点的第一行代码之前显示 TRUE:

After noticing it is TRUE, I tried to find the first point in the code where it becomes TRUE. Evidently, it is TRUE right from the very start. If I click F10 (step-over) to start debugging the program, and enter _CrtDumpMemoryLeaks() in the watch window, it shows TRUE even before the first line of code, in the entry point to the program:

extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                _In_ LPTSTR lpCmdLine, int nCmdShow)
#pragma warning(suppress: 4985)
{
    // call shared/exported WinMain
    return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}

另外,我推测调试工具可能在那个时候没有初始化并且 TRUE 是错误的.所以我在 OnInitDialog() 函数的第一行设置了一个断点,那里的值也是 TRUE.

Also, I speculated that maybe the debugging facilities are not initialized at that point and that the TRUE is erroneous. So I set a breakpoint at the first line in the OnInitDialog() function and there too the value is TRUE.

我怎么会在程序的早期出现内存泄漏?

How come I have memory leaks that early in the program?

推荐答案

您误解了返回值.TRUE 并不意味着内存泄漏,它意味着堆中有一些未释放的块,它们也可能被程序中的某些指针指向.这些对象可能由 CRT 启动代码和静态对象构造函数创建.

You misinterpret the return value. TRUE doesn't mean memory leak, it means there're some unreleased blocks in the heap, that might just as well be pointed to by some pointers in the program. Those objects might be created by CRT startup code and by the static objects constructors.

如果您仍然怀疑 - 设置分配hook 并检查对象何时创建.要尽早完成此操作,您需要一个在启动期间构造的对象 - 为此使用 #pragma init_seg( compiler ).

If you're still suspicious - set an allocation hook and inspect when objects are created. To do this early enough you'll need an object with constructed during startup - use #pragma init_seg( compiler ) for that.

这篇关于_CrtDumpMemoryLeaks( ) == 1 在第一行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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