vc不包含MFC就不打印内存泄露?

  无聊写了段测试代码,发现一个很费解的地方,下面这段代码是没有包含MFC的:

#include "stdafx.h"

struct stTest
{
    int a;
    int b;
    char szTest[32];
    stTest()
    {
        a = 1;
        b = 2;
        sprintf(szTest, "abc");
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    stTest* p = new stTest;
    printf("a = %d, b = %d, c = %s\n", p->a, p->b, p->szTest);

    getchar();

    return 0;
}

然后程序结束的时候并没有在输出面板打印内存泄露。如下图:

  如果程序包含了MFC,例如:

 

#include "stdafx.h"
#include "statci_new_test2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

struct stTest
{
    int a;
    int b;
    char szTest[32];
    stTest()
    {
        a = 1;
        b = 2;
        sprintf(szTest, "abc");
    }
};

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // 初始化 MFC 并在失败时显示错误
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: 更改错误代码以符合您的需要
        _tprintf(_T("致命错误: MFC 初始化失败\n"));
        nRetCode = 1;
    }
    else
    {
        // TODO: 在此处为应用程序的行为编写代码。
        static stTest* p = new stTest;
        printf("a = %d, b = %d, c = %s\n", p->a, p->b, p->szTest);

        getchar();
    }

    return nRetCode;
}

那结果就不一样了,输出面板打印了内存泄露的信息:

经过测试发现:如果程序包含了<afxwin.h>,那么程序结束时会在输出面板打印内存泄露信息,否则就不打印了。。。

 

原文地址:https://www.cnblogs.com/yuohoo/p/2972446.html