home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: cvnet.c++,comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Finding memory leaks
- Message-ID: <1992Nov23.210418.4537@taumet.com>
- Keywords: OOP,memory leaks
- Organization: TauMetric Corporation
- References: <1992Nov20.191828.14500@ultb.isc.rit.edu> <1992Nov22.181937.2176@taumet.com> <9232813.12737@mulga.cs.mu.OZ.AU>
- Date: Mon, 23 Nov 1992 21:04:18 GMT
- Lines: 49
-
- fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
-
- >steve@taumet.com (Steve Clamage) writes:
-
- >>mpk9172@ultb.isc.rit.edu (M.P. Kirby) writes:
- >>
- >>>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 ...
-
- >>>I would like to do the same kind of thing in C++...
-
- >>You do NOT want to replace "new" with a macro. "new" is
- >>a keyword which causes constructors to be called, and memory to be
- >>allocated via the function "operator new()". If you replace the "new"
- >>keyword with a macro, you destroy the semantics of most programs.
-
- >Unfortunately, we DO want to replace "new" with a macro, so that we can
- >get __LINE__ and __FILE__. Redefining operator new() is not sufficient, because
- >__LINE__ inside operator new() refers to the line where operator new() was
- >redefined, not the line where new was invoked. ...
-
- >The only way that I can see of solving the problem is to write a preprocessor
- >which properly parses the source code, and then does the desired replacements.
- >I don't see how it could be done using just the simple textual substitution
- >provided by the C preprocessor.
-
- If you don't mind modifying the source code, you can use placement syntax.
- For example, instead of
- T* p = new T;
- you would use
- T* p = new (__FILE__, __LINE__) T;
- The result would be a call to
- operator new(size_t size, char* file, int line)
- which would be your specialized allocation function.
-
- You could use a macro to make this less tedious:
- #define my_new(x) new (__FILE__, __LINE__) x
- ...
- T* p = my_new(T);
-
- Then write your own operator delete. You have to be aware that there may
- be calls to ordinary operator new from the library or other code. Your
- operator delete has to be able to cope with both, since there can be
- only one (global) operator delete in the system.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-