Article View: cvnet.c++
Article #6Re: Finding memory leaks
From: Fergus James HENDERSON
Date: Mon, 23 Nov 1992 02:47
Date: Mon, 23 Nov 1992 02:47
42 lines
2682 bytes
2682 bytes
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++. 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. > >There have been two answers to this question that completely missed >the point. 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 problem is not that "new" causes constructors to be called: we can solve that by invoking "new" from within the macro definition. Neither is the problem with recursive macros: as noted previously, they are quite OK. The problem is that the macro preprocessor is not capable of replacing all uses of keyword "new" which will cause calls to "::operator new()" without also affecting other uses of the "new" keyword. (Vector new and delete might also cause some other problems.) 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. -- Fergus Henderson fjh@munta.cs.mu.OZ.AU This .signature virus is a self-referential statement that is true - but you will only be able to consistently believe it if you copy it to your own .signature file!
Technical Details
Message-ID:
<9232813.12737@mulga.cs.mu.OZ.AU>Path:
arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjhReferences:
<1992Nov20.191828.14500@ultb.isc.rit.edu> <1992Nov22.181937.2176@taumet.com>Show Full Headers
Complete Article Headers
Newsgroups: cvnet.c++,comp.lang.c++ Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) Subject: Re: Finding memory leaks Message-ID: <9232813.12737@mulga.cs.mu.OZ.AU> Keywords: OOP,memory leaks Sender: news@cs.mu.OZ.AU Organization: Computer Science, University of Melbourne, Australia References: <1992Nov20.191828.14500@ultb.isc.rit.edu> <1992Nov22.181937.2176@taumet.com> Date: Mon, 23 Nov 1992 02:47:40 GMT Lines: 42