views:

189

answers:

5

Hi All,

I have a code of about 10,000 lines. I have to maintain a track for new and delete statements to check and avoid memory leaks. i can use new libraries or functions but i can't change the code. How can i do it? Please donot suggest for any memory cheking tool.

Any help would be appreciated.

+3  A: 

i can use new libraries or functions but i can't change the code.

Link to a heap implementation, which implements the global new and delete operators, and which keeps track of how many times each one is called.

HI ChrisW, Thanks for your reply. I implemented your suggessted way. it is working but i also want to know that, on which file and at which line these statements were used? Thanks in Advance

There are two ways to do that.

  1. The global new operator needs to look at (and perhaps decode) the call stack when it's invoked, to see/remember where it's being called from each time it's called.

  2. See the answers to Overriding “new” and Logging data about the caller

ChrisW
HI ChrisW,Thanks for your reply. I implemented your suggessted way. it is working but i also want to know that, on which file and at which line these statements were used? Thanks in Advance
max_dev
@max_dev I edited my answer, to answer your additional question.
ChrisW
Thanx a ton ChrisW
max_dev
A: 

On linux not sure about other OS you can use a tecnique called: interposing.

Gaetano Mendola
can you please explain that how can i use interpose? Thanks in advance
max_dev
A: 

You can override default C++ memory allocator, but you'd need to change your calls to new then. Another alternative is to override low-level function calls such as malloc, effectively that is what memory debugging libraries do (if that's your homework it's likely that you need to do exactly that). One more way is to change the code in a way so that you add a pointer to some set when you allocate memory in new, and then remove a pointer from the set when you call delete (that would be more a rookie way of doing things, but should work), and then check before existing application (or some other event when you expect your memory to be free), that you have an empty set of pointers.

Leonid
But i donot have to change my calls. vat i can do is to introduce some macrocs and function. i can use set but my application perhaps run for six months without exiting, in that case if there is any memory leaks then definitely my code will crash due to insufficient memory. Any ther idea?
max_dev
I can't see how you could do that without recompiling your code. Your symbol table needs to change if you want to override existing functions. In any architecture there is always a way to extend the functionality of your code, as it's one of most important questions asked when thinking about maintenance and extensibility of your code. If you're leaking memory, the best way is to use existing tools. There are books written about how to prevent memory leaks and how to troubleshoot them. The best way I've known so far is to use tools such as umem, or purify which saves you lots of time.
Leonid
A: 

Check here if you are on Windows/VS2010 combination

Chubsdad
+2  A: 

On Unix platform, you can use the LD_PRELOAD variable to substitute (at runtime) a library, this giving the opportunity to bring in you own new and delete operator.

This way, you can use implementation which will check for the allocations and deallocations, and you can (for example) print the callstack for every error.

Matthieu M.
Hi Matthieu,Thanks for your response. You suggessted me for LD_LIBRARY but will i not change new and delete call from my code in that case? please keep different scenarios of new and delete in mind.....
max_dev
You will change the functions that are called, but you will not have to change your code. In fact, you won't even have to recompile it! This facility allows to investigate third-party code for which you don't have the source.
Matthieu M.