We help IT Professionals succeed at work.

inexpensive C++ memory profiler

swsbuyer
swsbuyer asked
on
3,052 Views
Last Modified: 2013-12-12
Can you recommend a good C++ memory profiler and leak detector which is not too expensive.

Thanks.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
What OS do you use?
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
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."

Author

Commented:
Sorry. I use windows & microsoft visual studio for development.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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")
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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/Programming/Editors_IDEs/C_CPP_CS/Visual_CPP/Q_23017807.html?cid=236#a20461542

-Rx.

Author

Commented:
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?
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
Valgrind is a Linux only tool (sadly) -- it's worth it's virtual weight in gold!
CERTIFIED EXPERT
Top Expert 2012
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.