Link to home
Start Free TrialLog in
Avatar of swsbuyer
swsbuyer

asked on

inexpensive C++ memory profiler

Can you recommend a good C++ memory profiler and leak detector which is not too expensive.

Thanks.
Avatar of krishnadevank
krishnadevank

Avatar of pepr
What OS do you use?
You don't state your OS; however, if it's Linux consider using Valgrind.

http://www.valgrind.org/

"Valgrind is an award-winning suite of tools for debugging and profiling Linux programs. With the tools that come with Valgrind, you can automatically detect many memory management and threading bugs, avoiding hours of frustrating bug-hunting, making your programs more stable. You can also perform detailed profiling, to speed up and reduce memory use of your programs.

The Valgrind distribution currently includes four tools: a memory error detector, a cache (time) profiler, a call-graph profiler, and a heap (space) profiler. It runs on the following platforms: X86/Linux, AMD64/Linux, PPC32/Linux, PPC64/Linux.

Valgrind is Open Source / Free Software, and is freely available under the GNU General Public License."
Avatar of swsbuyer

ASKER

Sorry. I use windows & microsoft visual studio for development.
On the other hand, if you really want to save money, there is plenty you can do on your own. Add the following to a central header file:

#ifdef _DEBUG
#ifndef _DBG_NEW

#include <crtdbg.h>

inline void* __operator_new(size_t __n) {
     return ::operator new(__n,_NORMAL_BLOCK,__FILE__,__LINE__);
}
inline void* _cdecl operator new(size_t __n,const char* __fname,int __line) {
     return ::operator new(__n,_NORMAL_BLOCK,__fname,__line);
}
inline void _cdecl operator delete(void* __p,const char*,int) {
     ::operator delete(__p);
}

#define _DBG_NEW new(__FILE__,__LINE__)
#define new _DBG_NEW


#endif // _DBG_NEW
#else

#define __operator_new(__n) operator new(__n)

#endif

Then, add

               int tmpFlag;

               // Get the current state of the flag
               // and store it in a temporary variable
               tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );

               // Turn On (OR) - Keep freed memory blocks in the
               // heap’s linked list and mark them as freed
               tmpFlag |= _CRTDBG_LEAK_CHECK_DF;

               // Set the new state for the flag
               _CrtSetDbgFlag( tmpFlag );


to your app's start code and call

               _CrtDumpMemoryLeaks ();


to before the program ends and you'll get the line number where the allocation occured.

(Ref.: http:Q_21009673.html)

Regarding profiling: You can add the profiling feature to your code yourself, the profiler does nothing else but adding prolog/epilog code regarding the timing issue, that is called 'Automatic Code Instrumentation'. See http://www.johnpanzer.com/aci_cuj/index.html ("Automatic Code Instrumentation")
>> On the other hand, if you really want to save money
Just to clarify, Valgrind is free.

Also, there is a difference between memory profiling and leak detection; however, if you are after the latter then jkr is right, there is much you can do in Visual Studio (assuming you are developing for Windows using Visual Studio).

You can use _CrtSetDbgFlag to enable CRT heap allocation debugging. This should be at the very start of your program.
E.g. _CrtSetDbgFlag ( _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) | _CRTDBG_LEAK_CHECK_DF );
http://msdn2.microsoft.com/en-us/library/974tc9t1(VS.80).aspx

You can use _CrtDumpMemoryLeaks to generate an error report if the application failed to free all the memory it allocated. This should be at the very end of your program.
http://msdn2.microsoft.com/en-us/library/d41t22sb(VS.80).aspx

Use can use _CrtSetBreakAlloc or _crtBreakAlloc to set break points where specific heap is allocated (as reported by CrtDumpMemoryLeaks) so that you can see where the problem starts
http://support.microsoft.com/kb/151585

Memory leak detection and isolation: http://msdn2.microsoft.com/en-us/library/x98tx3cf(VS.80).aspx

https://www.experts-exchange.com/questions/23017807/Detecting-memory-leaks.html?cid=236&anchorAnswerId=20461542#a20461542

-Rx.
Thanks all.

In fact I am looking for both memory leak detection as well as memory corruptions. Also, the application in question is a server like application which wait for requests and processes those. Hence, there is no logical end for the application. So far, the cheapest solution I have found which does not require one to do a whole bunch of programming is AQTime.

I am upto adding some code, but cannot afford (timewise) to do a lot there. Do you think that something like Valgrind is available on windows?
Valgrind is a Linux only tool (sadly) -- it's worth it's virtual weight in gold!
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial