zeroboo
zeroboo

Reputation: 8995

C++ memory leak detecting method

I'm working on a project using many external library on windows. I got problem with memory leak: i detected many memory leaks by overriding operator new/new[] and delete/delete[]. The problem is i know how many memory blocks are leaked, but don't know where to find them, in overrided functions, i could log size and position of allocated mem block, without stack trace.

So to deal with it, i guess i need to log stack trace too (but how?), or is there any way to find which code caused memory leaked?

Thanks a lot for any help.

Upvotes: 1

Views: 654

Answers (2)

Sunscreen
Sunscreen

Reputation: 3564

There are also COTS that can show memory leaks for you such as Rational Purify (http://www-01.ibm.com/software/awdtools/purify/win/). I m mentioning that as we used it in my last position.

I suppose there are also free ones. Please mention if any.

Upvotes: 0

Andreas Brinck
Andreas Brinck

Reputation: 52549

I use the following approach to supply new with info on which file and line that allocates each memory block:

void operator delete(void *p, const char* filename, int line);
void operator delete(void *p, const char* filename, int line, const std::nothrow_t&);
void operator delete[](void *p, const char* filename, int line);
void operator delete[](void *p, const char* filename, int line, const std::nothrow_t&);

void *operator new(std::size_t n, const char* filename, int line);
void *operator new(std::size_t n, const std::nothrow_t&, const char* filename, int line);
void *operator new[](std::size_t n, const char* filename, int line);
void *operator new[](std::size_t n, const std::nothrow_t&, const char* filename, int line);

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

Upvotes: 3

Related Questions