Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Leak Detecting and overriding new?

I am attempting to get Memory leak detection working with the help of these two articles: http://msdn.microsoft.com/en-us/library/e5ewb1h3%28VS.80%29.aspx http://support.microsoft.com/kb/q140858/

So in my stdafx.h I now have:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#define new new(_NORMAL_BLOCK,__FILE__,__LINE__)

The only problem is, I have a class which overrides the new function:

class Dummy
{    
  //overloaded new operator
  void FAR* operator new(size_t cb);
}

Now when I compile this code, I get: error C2059: syntax error : 'constant' error C2091: function returns function

Any idea how I can fix this?

like image 771
Kyle Avatar asked Jul 14 '09 22:07

Kyle


People also ask

How do you detect memory leaks?

The primary tools for detecting memory leaks are the C/C++ debugger and the C Run-time Library (CRT) debug heap functions. The #define statement maps a base version of the CRT heap functions to the corresponding debug version. If you leave out the #define statement, the memory leak dump will be less detailed.

How do you fix a memory leak in production?

Fixing Memory Leak Using Heap Dump: It is a technique that is the solution to the memory leak problem. It is a snapshot of all objects that reside in the memory at a certain time. It also optimizes memory usage in a Java application.

What is the main cause of memory leaks in applications?

Holding the references of the object and resources that are no longer needed is the main cause of the memory leaks in android applications. As it is known that the memory for the particular object is allocated within the heap and the object point to certain resources using some object reference.

How do you detect memory leaks in performance testing?

The best approach to checking for the existence of a memory leak in your application is by looking at your RAM usage and investigating the total amount of memory been used versus the total amount available. Evidently, it is advisable to obtain snapshots of your memory's heap dump while in a production environment.


2 Answers

You can use pragma directives to save and restore the new macro when undefing for overloads. See [MSDN](http://msdn.microsoft.com/en-us/library/hsttss76(VS.71).aspx) for the exact syntax.

E.g.

#pragma push_macro("new")
#undef new
void FAR* operator new(size_t cb);
#pragma pop_macro("new") 

You can put these in headers, e.g.

begin_new_override.h:

#ifdef new
#define NEW_WAS_DEFINED
#pragma push_macro("new")
#undef new
#endif

end_new_override.h:

#ifdef NEW_WAS_DEFINED
#undef NEW_WAS_DEFINED
#pragma pop_macro("new")
#endif

And then

#include "begin_new_override.h"
void FAR* operator new(size_t cb);
#include "end_new_override.h"
like image 151
Logan Capaldo Avatar answered Sep 29 '22 11:09

Logan Capaldo


Redefining new via #define at the preprocessor level is a bad idea in my experience -- you not only break operator new overloads, but also placement new, and probably a few other things.

Having all those FILE and LINE macros expanding everywhere causes your .rodata and .data sections to bloat with file strings and line numbers, and generates much more code per call.

Much better (if more effort up-front) to take advantage of the existence of debug information (e.g. .pdb file) and use something like the DbgHelp library's StackWalk64 to gather stack info.

Overload the various combinations of global operator new and operator delete (array, nothrow, etc), have them store and release stack information as memory is allocated and freed.

You can even store this information in a structure like a std::map<void *, StackInfo>, just be careful to not record the allocs caused by the map inserts (a global lock may be sufficient for a single-threaded app, multi-threaded is left as an exercise to the reader).

Since you're recording the entire stack for any given alloc, you can do some nifty tree analysis, grouping allocations (leaks or otherwise) by "function and descendants"... And it is sometimes easier to trace complicated leaks if you know the entire stack from their allocation time.

like image 40
leander Avatar answered Sep 29 '22 11:09

leander