1

I have a simple question: How can I trace memory leaks in a VS 2010 MFC C++ project?

For debug builds there is the possibility described here.

What is the solution for release build without having to use 3rd party tools/projects?

macilaci123
  • 89
  • 1
  • 9

2 Answers2

0

The only solution is to override memory allocation operators (new and delete) yourself; these operators may log any memory allocation and deallocation to some kind of log, so you'll cal analyze this log later. You can see details here: Overriding memory allocation method standard libraries use? However, this will affect speed terribly. I am not sure it is possible to override free and malloc functions also. You can use #define to replace standard free and malloc in your own code, but I am not sure it is possible for your library dependencies, so your memory allocation/deallocation log may miss data allocated by library dependencies. Of course, you can rebuild all libraries with your own memory management functions.

Community
  • 1
  • 1
Vitalii
  • 4,434
  • 4
  • 35
  • 77
0

I have tried to use MFC memory leak tools, but they only work in Debug. I have tried to use various tool apps like deleaker, but it is expensive. There are also free tools on GitHub, but the one I have tried was out of date (code unmaintained) which cost a bunch of time to configure and set up and just wasn't worth it.

Believe it or not, the best "tool" I have found is program component isolation through iterative compiling. Simply walk through your code, and disable/comment out code, from large to small. In other words, you comment out all the code, there is no leak, etc etc. Recompile and let the leak dump tell you if there is a leak or not. You can do this until the erring code jumps out at you. It is surprisingly effective. Sometimes the simplest solution is the best.

I assume you have a way of detecting leaks in Release mode, so this approach could work for that as well. I don't remember if Release MFC has leak dump or not. Maybe someone else knows this off the top of their head.

rtischer8277
  • 496
  • 6
  • 27