How do you detect/avoid Memory leaks in your (Unma

2019-01-04 04:51发布

In unmanaged C/C++ code, what are the best practices to detect memory leaks? And coding guidelines to avoid? (As if it's that simple ;)

We have used a bit of a silly way in the past: having a counter increment for every memory allocation call and decrement while freeing. At the end of the program, the counter value should be zero.

I know this is not a great way and there are a few catches. (For instance, if you are freeing memory which was allocated by a platform API call, your allocation count will not exactly match your freeing count. Of course, then we incremented the counter when calling API calls that allocated memory.)

I am expecting your experiences, suggestions and maybe some references to tools which simplify this.

29条回答
戒情不戒烟
2楼-- · 2019-01-04 05:18

I would recommend using Memory Validator from software verify. This tool proved itself to be of invaluable help to help me track down memory leaks and to improve the memory management of the applications i am working on.

A very complete and fast tool.

查看更多
地球回转人心会变
3楼-- · 2019-01-04 05:21

Most memory profilers slow my large complex Windows application to the point where the results are useless. There is one tool that works well for finding leaks in my application: UMDH - http://msdn.microsoft.com/en-us/library/ff560206%28VS.85%29.aspx

查看更多
迷人小祖宗
4楼-- · 2019-01-04 05:21

I'd like to offer something I've used at times in the past: a rudimentary leak checker which is source level and fairly automatic. I'm giving this away for three reasons:

  1. You might find it useful.

  2. Though it's a bit krufty, I don't let that embarass me.

  3. Even though it's tied to some win32 hooks, that should be easy to alleviate.

There are things of which you must be careful when using it: don't do anything that needs to lean on new in the underlying code, beware of the warnings about cases it might miss at the top of leakcheck.cpp, realize that if you turn on (and fix any issues with) the code that does image dumps, you may generate a huge file.

The design is meant to allow you to turn the checker on and off without recompiling everything that includes its header. Include leakcheck.h where you want to track checking and rebuild once. Thereafter, compile leakcheck.cpp with or without LEAKCHECK #define'd and then relink to turn it on and off. Including unleakcheck.h will turn it off locally in a file. Two macros are provided: CLEARALLOCINFO() will avoid reporting the same file and line inappropriately when you traverse allocating code that didn't include leakcheck.h. ALLOCFENCE() just drops a line in the generated report without doing any allocation.

Again, please realize that I haven't used this in a while and you may have to work with it a bit. I'm dropping it in to illustrate the idea. If there turns out to be sufficient interest, I'd be willing to work up an example, updating the code in the process, and replace the contents of the following URL with something nicer that includes a decently syntax-colored listing.

You can find it here: http://www.cse.ucsd.edu/~tkammeye/leakcheck.html

查看更多
对你真心纯属浪费
5楼-- · 2019-01-04 05:22

Valgrind is a nice option for Linux. Under MacOS X, you can enable the MallocDebug library which has several options for debugging memory allocation problems (see the malloc manpage, the "ENVIRONMENT" section has the relevant details). The OS X SDK also includes a tool called MallocDebug (usually installed in /Developer/Applications/Performance Tools/) that can help you to monitor usage and leaks.

查看更多
姐就是有狂的资本
6楼-- · 2019-01-04 05:22

There are various replacement "malloc" libraries out there that will allow you to call a function at the end and it will tell you about all the unfreed memory, and in many cases, who malloced (or new'ed) it in the first place.

查看更多
走好不送
7楼-- · 2019-01-04 05:23

As a C++ Developer here's some simply guidelines:

  1. Use pointers only when absolutely necessary
  2. If you need a pointer, doublecheck if a SmartPointer is a possibility
  3. Use the GRASP Creator pattern.

As for the detection of memory leaks personally I've always used Visual Leak Detector and find it to be very useful.

查看更多
登录 后发表回答