内存泄漏检测文件错误

5

我有一个程序,应该输出它的内存泄漏信息。然而,它没有工作。以下是程序:

#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;
}

我正在使用Visual Studio 2010编译DEBUG版本,因此函数不应被忽略。该程序只创建一个文件“Memory Leaks.txt”,但文件中没有任何内容。有什么想法吗?
--编辑--
我已经根据建议更新了程序以使用“proper File Handle”(正确的文件句柄)。该程序仍然未向文件输出任何内容。
--编辑--
问题出在关闭文件上。以下代码现在可以工作。
#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;
}

1
我完全不了解这些API,但是似乎很奇怪的是你正在操作stdout,但是传递的标志名称包含STDERR - Mat
3
请勿在泄漏报告生成之前关闭日志文件。换句话说,不要关闭文件。 - Hans Passant
好的,看起来运行正常。 - GILGAMESH
@HansPassant 是的,我应该在我的答案中明确说明这一点... - JasonD
如果您以某种不在现有答案中涵盖的方式解决了问题,请_发布您自己的答案_详细说明最终解决方案,并接受它。 - Lightness Races in Orbit
显示剩余6条评论
3个回答

6

停止在GUI Windows应用程序中尝试重定向stderr或stdout,并打开适当的文件句柄,这只需要一行代码。

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!


0

0

试试这个替代方案

#include < crtdbg.h >
#define _CRTDBG_MAP_ALLOC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接