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

Discussion in 'C++' started by Lighter, Aug 18, 2006.

  1. Lighter

    Lighter Guest

    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.
    Lighter, Aug 18, 2006
    #1
    1. Advertising

  2. Lighter

    Alan Johnson Guest

    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
    Alan Johnson, Aug 18, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chinmoy Mukherjee

    Memory leak detector for C++

    Chinmoy Mukherjee, Apr 26, 2005, in forum: C++
    Replies:
    4
    Views:
    668
    Raymond Martineau
    Apr 27, 2005
  2. Stephen Kellett

    Java Memory Leak Detector

    Stephen Kellett, Oct 19, 2006, in forum: Java
    Replies:
    0
    Views:
    636
    Stephen Kellett
    Oct 19, 2006
  3. Stephen Kellett

    Python Memory Leak Detector

    Stephen Kellett, Oct 19, 2006, in forum: Python
    Replies:
    0
    Views:
    631
    Stephen Kellett
    Oct 19, 2006
  4. Stephen Kellett

    Ruby Memory Leak Detector

    Stephen Kellett, Oct 19, 2006, in forum: Ruby
    Replies:
    0
    Views:
    173
    Stephen Kellett
    Oct 19, 2006
  5. ara.t.howard
    Replies:
    18
    Views:
    249
    Eric Hodel
    Oct 4, 2007
Loading...

Share This Page