内存泄漏检测文件错误

时间:2013-01-01 17:09:16

标签: c++ visual-studio-2010 visual-c++ memory-leaks crtdbg.h

我有一个程序应该输出内存泄漏的信息。但是,它不起作用。以下是该计划:

#include <crtdbg.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    FILE *out_file;
    int *a = new int;

    //Redirect the error stream to a file.
    freopen_s (&out_file, "Memory Leaks.txt", "w", stderr);

    //Turn on debugging for memory leaks. This is automatically turned off when the build is Release.
    _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    _CrtSetReportMode (_CRT_WARN, _CRTDBG_MODE_FILE);
    _CrtSetReportFile (_CRT_WARN, _CRTDBG_FILE_STDERR);
    _CrtSetReportMode (_CRT_ERROR, _CRTDBG_MODE_FILE);
    _CrtSetReportFile (_CRT_ERROR, _CRTDBG_FILE_STDERR);
    _CrtSetReportMode (_CRT_ASSERT, _CRTDBG_MODE_FILE);
    _CrtSetReportFile (_CRT_ASSERT, _CRTDBG_FILE_STDERR);

    return 0;
}

我在DEBUG版本中构建,因此不应忽略这些功能。我使用的编译器是Visual Studio 2010.该程序只创建一个文件“Memory Leaks.txt”,但文件中没有内容。有什么想法吗?

- 编辑 -

我已根据建议更新程序以使用“正确的文件句柄”。该程序仍然没有输出任何文件。

- 编辑 -

问题在于关闭文件。以下代码现在可以使用。

#include <crtdbg.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HANDLE hLogFile;
    int *a;

    //Open a file for output.
    hLogFile = CreateFile ("Memory Leaks.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    //Turn on debugging for memory leaks. This is automatically turned off when the build is Release.
    _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    _CrtSetReportMode (_CRT_WARN, _CRTDBG_MODE_FILE);
    _CrtSetReportFile (_CRT_WARN, hLogFile);
    _CrtSetReportMode (_CRT_ERROR, _CRTDBG_MODE_FILE);
    _CrtSetReportFile (_CRT_ERROR, hLogFile);
    _CrtSetReportMode (_CRT_ASSERT, _CRTDBG_MODE_FILE);
    _CrtSetReportFile (_CRT_ASSERT, hLogFile);

    //Create a memory leak.
    a = new int;

    //Don't close this file. Closing the file will cause the report not to be outputted.
    //CloseHandle(hLogFile);

    return 0;
}

3 个答案:

答案 0 :(得分:5)

停止绑定以在GUI Windows应用程序中重定向stderr 标准输出,并打开正确的文件句柄。这是一个单行。

HANDLE hLogFile = CreateFile(L"Memory Leaks.txt", GENERIC_WRITE, FILE_SHARE_WRITE, 
  NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

//Turn on debugging for memory leaks. This is automatically turned off when the build is Release.
_CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetReportMode (_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile (_CRT_WARN, hLogFile);
_CrtSetReportMode (_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile (_CRT_ERROR, hLogFile);
_CrtSetReportMode (_CRT_ASSERT, _CRTDBG_MODE_FILE);
_CrtSetReportFile (_CRT_ASSERT, hLogFile);

在实际生成报告之前不要关闭HANDLE!

答案 1 :(得分:0)

应该用于输出流 stderr

//Redirect the error stream to a file.
freopen_s (&out_file, "Memory Leaks.txt", "w", stderr);

可以找到示例: http://msdn.microsoft.com/en-us/library/a68f826y%28v=VS.71%29.aspx

答案 2 :(得分:0)

试试这个

#include < crtdbg.h >
#define _CRTDBG_MAP_ALLOC