Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Leak Detection File Error

I have a program that is supposed to output info on its memory leaks. However, it is not working. Following is the program:

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

I am building in the DEBUG version, so the functions shouldn't be ignored. The compiler I am using is Visual Studio 2010. The program only creates a file "Memory Leaks.txt" but there is no content in the file. Any thoughts?

--EDIT--

I have updated the program to use a "proper File Handle" as suggested. The program still outputs nothing to the file.

--EDIT--

The problem was with closing the file. The following code now works.

#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;
}
like image 203
GILGAMESH Avatar asked Jan 01 '13 17:01

GILGAMESH


1 Answers

Stop tying to redirect stderr or stdout in a GUI Windows app, and open a proper File Handle. it's a one-liner.

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);

And don't close the HANDLE before the reports are actually generated!

like image 69
JasonD Avatar answered Sep 28 '22 16:09

JasonD