View _CrtDumpMemoryLeaks and printf?

Started by
3 comments, last by Gumgo 17 years, 2 months ago
I have a window application, and I need to know how to view the dump from _CrtDumpMemoryLeaks... and also printf. How do I output all this info to a log at the end of the program (or something similar)? Thanks
Advertisement
_CrtDumpMemoryLeaks will dump memory leaks to the Output window of Visual Studio after the program terminates. This happens only after running the program under the debugger (F5).

printf will output to your stdout ie. console window (if there is one).

Yes you can redirect _CrtDumpMemoryLeaks to your console window or to a file if you want.

This will dump to a console.
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);_CrtDumpMemoryLeaks();



You can use freopen to direct the dump to a file.
FILE *pFile;freopen_s( &pFile, "c:/log2.txt", "w", stdout);_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);_CrtDumpMemoryLeaks();fclose(pFile);


You leaks will get output to log2.txt
++ My::Game ++
Hmm... I tried that and intentionally made a memory leak to test it and it was detected but didn't output anything.

One thing though, is that I couldn't get the "_s" part to work on freopen_s, so I'm just using freopen.
Quote:
One thing though, is that I couldn't get the "_s" part to work on freopen_s, so I'm just using freopen.

That is perfectly ok. I use Visual C++ express (VC8), you must be using an older compiler. freopen was deprecated.

Quote:
Hmm... I tried that and intentionally made a memory leak to test it and it was detected but didn't output anything.

Are you using "debug" version of the runtime? Try running it under the debugger "F5". Also _CrtDumpMemoryLeak needs to be called at the very end of the program.

Also, you posted 3 times by mistake. Delete the posts. Select, edit on the posts and click on delete option on the top.
++ My::Game ++
Yeah, sorry about posting three times. Sometimes the forum doesn't work right and I can't edit or delete posts. Anyways, thanks for the advice and I'll try deleting them now.

EDIT: Hmmm... for some reason the _CrtDumpMemoryLeaks is returning a memory leak but the functions for taking and comparing memory "snapshots" aren't... I read somewhere that it returns false positives, is this true? Which is more reliable? The memory leak dump function or the snapshot compare function?

[Edited by - Gumgo on February 1, 2007 11:54:45 PM]

This topic is closed to new replies.

Advertisement