Article View: cvnet.c++
Article #3Finding memory leaks
From: M.P. Kirby
Date: Fri, 20 Nov 1992 19:18
Date: Fri, 20 Nov 1992 19:18
33 lines
2025 bytes
2025 bytes
In C I used to use a memory leak finder that would help me find my memory leaks. I basically created a #define for malloc that redefined it to do: #define malloc(size) (my_malloc(__FILE__,__LINE__,size) This allowed me to maintain a list of all my memory allocations. I would redefine free to be my_free to mark the element on the list as "freed." This allowed me to check to see what memory was outstanding at different points in the program. (and at the end). I would then be able to find out that a block of 300 k was allocated in file "stuff.c" at line 53. This proved useful during the debuging stage. I would like to do the same kind of thing in C++. Unfortunately, it seems that new is not as easy to #define over as malloc. I thought (briefly) that I could do something like #define new new(__LINE__,__FILE__) and then overload the new operator, but this causes a recursive macro. Creating something that calls my_new doesn't work either. Apparently the sytanx surround the new operator is special. Is there anyway to do what I am trying to accomplish? Actually, What I would really want is a stack dump at every memory allocation. This way I would not only know that a block was allocated at a certain line number and in a file, but it was ordered by these people. But I'm willing to take the easy way out. Just knowing that I have a memory leak and where it is occuring is good enough. Mike Kirby mpk9172@ultb.isc.rit.edu Rochester Institute of Technology
Technical Details
Message-ID:
<1992Nov20.191828.14500@ultb.isc.rit.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!spool.mu.edu!darwin.sura.net!udel!rochester!rit!isc-newsserver!mpk9172Show Full Headers
Complete Article Headers
Newsgroups: cvnet.c++,comp.lang.c++ Path: arc1-ca.pugleaf.net!sparky!uunet!spool.mu.edu!darwin.sura.net!udel!rochester!rit!isc-newsserver!mpk9172 From: mpk9172@ultb.isc.rit.edu (M.P. Kirby) Subject: Finding memory leaks Message-ID: <1992Nov20.191828.14500@ultb.isc.rit.edu> Originator: mpk9172@ultb Keywords: OOP,memory leaks Sender: news@ultb.isc.rit.edu (USENET News System) Nntp-Posting-Host: ultb-gw.isc.rit.edu Organization: Rochester Institute of Technology Date: Fri, 20 Nov 1992 19:18:28 GMT Lines: 33