Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is there a way to write a memory leak detector supporting new(nothrow)?

Reply
Thread Tools

Is there a way to write a memory leak detector supporting new(nothrow)?

 
 
Lighter
Guest
Posts: n/a
 
      08-18-2006
Is there a way to write a memory leak detector supporting new(nothrow)?

For example,

#include <My_Debug_New.h>

using namespace std;

int main()
{
int* p1 = new int;
int* p2 = new(nothrow) int; // note this!!!
}

Ideally, after running it in debug mode, owing to p1 and p2 are not
deleted, the output window of the IDE should report memory leaks with
source file names and actual line numbers.

Provided that the whole program doesn't use new(nothrow), I can
implement a memory leak detector as follows:

#if _DEBUG
void* operator new(size_t size, char* srcFileName, int nLineNum);
void* operator delete(void* p);
// ......
#define new new(__FILE__, __LINE__)
#endif

However, by using macro, new and new(nothrow) cannot be simultaneouly
supported. My question is: How to implement this feature that can
simultaneously support? Is this feasible?

Thanks in advance for any help.

 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      08-18-2006
Lighter wrote:
> Is there a way to write a memory leak detector supporting new(nothrow)?
>
> For example,
>
> #include <My_Debug_New.h>
>
> using namespace std;
>
> int main()
> {
> int* p1 = new int;
> int* p2 = new(nothrow) int; // note this!!!
> }
>
> Ideally, after running it in debug mode, owing to p1 and p2 are not
> deleted, the output window of the IDE should report memory leaks with
> source file names and actual line numbers.
>
> Provided that the whole program doesn't use new(nothrow), I can
> implement a memory leak detector as follows:
>
> #if _DEBUG
> void* operator new(size_t size, char* srcFileName, int nLineNum);
> void* operator delete(void* p);
> // ......
> #define new new(__FILE__, __LINE__)
> #endif
>
> However, by using macro, new and new(nothrow) cannot be simultaneouly
> supported. My question is: How to implement this feature that can
> simultaneously support? Is this feasible?
>
> Thanks in advance for any help.
>


Overloading new and delete to detect memory leaks is a brittle solution.
Using macros to redefine a keyword is more brittle. At that point you
are relying on clients to correctly use one thing (your redefined new)
to detect incorrectly using another thing (ironically, the same thing
you are relying on them to use correctly).

A better solution is to use some memory debugger like Valgrind or
Purify. There is a list of memory debuggers on wikipedia:
http://en.wikipedia.org/wiki/Memory_debugger

--
Alan Johnson
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Memory Leak Detector Stephen Kellett Python 0 10-19-2006 11:05 AM
Java Memory Leak Detector Stephen Kellett Java 0 10-19-2006 11:05 AM
Memory leak detector for C++ Chinmoy Mukherjee C++ 4 04-27-2005 03:54 AM



Advertisments