Memory leak in a windows service (ATL COM exe Service)

Kwesi Ackon
Kwesi Ackon used Ask the Experts™
on
I have a memory leak in a windows service (ATL COM exe Service),  built using Visual Studio 2010, running on a Windows Server 2003.
Does anyone know of any memory leak detector tools than I can use to detect the leaks down to a variable level (e.g. show the pointer variable causing the leaks).
Is a critical production issue , any help will be greatly appreciated.  I know googling will help me, but I'm running against time, and I don't have the luxury of time to try
all the tools out there, before choosing the best, so if anyone has had to perform similar diagnosis, please let me know.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun VermaakSenior Consultant
Awarded 2017
Distinguished Expert 2018

Commented:
Do you have the source code?

I am a big fan of redgate products. Perhaps ANTS can pinpoint the memory leak
https://www.red-gate.com/products/dotnet-development/ants-memory-profiler/walkthrough
Top Expert 2016

Commented:
how did you know that there is a leak? the value you got from task manager is not reliable especially if running in debug mode. a service has the disadvantage that it runs in a loop. for each cycle it may create some kind of leaks which are due to heap storage that is too short to be reused by new object creation. actually you can't avoid this beside of using your own heap (memory management). if the memory used grows slowly, the best you could do is to restart the service from time to time.

if you think it is a real leak produced by operator new or malloc, you may use a smart pointer instead of a real pointer, what should help from leaks. or use a std::set<void*> where you add each pointer created. if you free (delete) the pointer erase it from std::set. after end of the service the std::set would contain all pointers not freed. if there are some, you would replace the std::set by a std::map that maps the pointer to a unique integer which you add together with the pointer when memory was allocated. the contents of the map would show you all leaks together with a number where the pointer was created.

Sara
Kwesi AckonC++ Developer

Author

Commented:
Hi Sarabande, sorry for delayed response, been very busy calming down issues:

You mention:

for each cycle it may create some kind of leaks which are due to heap storage that is too short to be reused by new object creation

"too short", are you referring to size or speed of creation? I wrote a test application, and  I noticed if you allocate memory fast enough , i.e., Sleep for 10 milliseconds, even if you deallocate in that same process after allocating, memory is still leaked. This is for memory within the service. Thanks in advance.
Top Expert 2016

Commented:
Actually, if you refer to the output shown in the task manager, those figures are not reliable. they actually count only memory storage explicitly requested or freed. they do not count losses which is due to alignment mismatches (if a program allocates memory where the size is not a multiple of 8 or 16) and they do not count small deallocations. especially in debug mode, task manager is not suitable to detect memory leaks. note, if you run your program from visual studio ide, you would see real leaks in the output window. if you don't see any, it is not necessary an evidence that there are no leaks at all but at least it is not a simple leak like heap memory allocated but not freed at all.

Sara

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial