Jump to content
  • Advertisement
Sign in to follow this  
Gumgo

View _CrtDumpMemoryLeaks and printf?

This topic is 4159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!