How to replace delete[] with a macro

Discussion in 'C++' started by foo, Jun 23, 2003.

  1. foo

    foo Guest

    I'm creating a debug class called debug_mem_allocation for the purpose
    of finding memory leaks.
    I used macro's to replace the new and delete operators.
    My problem is with trying to replace the delete[] operator with a
    macro.

    I can't replace the delete[] operator by using void* as the first
    parameter, because then my code will not be able to modify the calling
    function's pointer, nor would it get the source file name and line
    number.
    My current work around has been to modify the source code by replacing
    delete[] with DELETE_ARRAY_AND_NULLIFY, which is defined as delete[]
    for NON-DEBUG compile, and defined as an operator-() of my debug class
    for DEBUG compile.

    Can anyone think of a better way to replace the delete[] operator via
    macro that would not require me to modify the source code?

    My compilers are VC++ 6.0 and GNU 3.x

    Here's the header:

    class debug_mem_allocation
    {
    public:
    enum ACTION_SWITCH{AllocateMem, DeAllocMem, GenerateReport};
    debug_mem_allocation(const char* FileName, int LineNo, const char*
    FunctionName)
    :m_FileName(FileName), m_LineNo(LineNo), m_FuncName(FunctionName){}
    template<typename T>
    void operator+(T &ptr)
    {
    Log(ptr, DeAllocMem);
    delete ptr;
    ptr = NULL;
    }
    template<typename T>
    void operator+(const T &ptr)
    {
    Log(ptr, DeAllocMem);
    delete ptr;
    }
    template<typename T>
    void operator-(T &ptr)
    {
    Log(ptr, DeAllocMem);
    delete [] ptr;
    ptr = NULL;
    }
    template<typename T>
    T& operator<<(T &ptr)
    {
    Log(ptr, AllocateMem);
    return ptr;
    }
    template<typename T>
    void free_and_nullify(T &memblock , const char* VariableName)
    {
    Log(memblock, DeAllocMem);
    free(memblock);
    memblock = NULL;
    }
    void *malloc_and_log(size_t size );
    void *realloc_and_log(void *memblock, size_t size );
    static void SendOutCurrentLog();
    private:
    void Log(void const * const Ptr, ACTION_SWITCH Sw);
    const char* m_FileName;
    const int m_LineNo;
    const char* m_FuncName;
    };


    #if (defined(DEBUG) || defined (_DEBUG)) &&
    defined(USE_DEBUG_MEM_ALLOCATION)
    #define new debug_ext::debug_mem_allocation(__FILE__, __LINE__,
    __FUNCTION__)<< new
    #define delete debug_ext::debug_mem_allocation(__FILE__,
    __LINE__, __FUNCTION__)+
    #define DELETE_ARRAY_AND_NULLIFY debug_ext::debug_mem_allocation(__FILE__,
    __LINE__, __FUNCTION__)-
    #define malloc(x) debug_ext::debug_mem_allocation(__FILE__,
    __LINE__, __FUNCTION__).malloc_and_log(x)
    #define realloc(m,s) debug_ext::debug_mem_allocation(__FILE__,
    __LINE__, __FUNCTION__).realloc_and_log(m,s)
    #define free(x) debug_ext::debug_mem_allocation(__FILE__,
    __LINE__, __FUNCTION__).free_and_nullify(x, #x)
    #else
    #define DELETE_ARRAY_AND_NULLIFY delete []
    #endif
    foo, Jun 23, 2003
    #1
    1. Advertising

  2. foo

    Ron Natalie Guest

    "foo" <> wrote in message news:...
    > I'm creating a debug class called debug_mem_allocation for the purpose
    > of finding memory leaks.
    > I used macro's to replace the new and delete operators.
    > My problem is with trying to replace the delete[] operator with a
    > macro.


    You can't replace delete[] with a macro. You can only replace identifires.
    Furthermore your code, even with delete, is fraught with perils.
    Nothing says that delete has to be given a modifable lvalue.
    Ron Natalie, Jun 23, 2003
    #2
    1. Advertising

  3. foo

    foo Guest

    "Ron Natalie" <> wrote in message news:<>...
    > "foo" <> wrote in message news:...
    > > I'm creating a debug class called debug_mem_allocation for the purpose
    > > of finding memory leaks.
    > > I used macro's to replace the new and delete operators.
    > > My problem is with trying to replace the delete[] operator with a
    > > macro.

    >
    > You can't replace delete[] with a macro. You can only replace identifires.
    > Furthermore your code, even with delete, is fraught with perils.
    > Nothing says that delete has to be given a modifable lvalue.



    That's why there's an overloaded version of operator+()
    template<typename T>
    void operator+(const T &ptr)
    {
    Log(ptr, DeAllocMem);
    delete ptr;
    }
    The overloaded version with the const does not try to set the pointer
    to a NULL, but it does do the logging.
    If delete is given an rvalue, this operator is used instead of the
    following:
    template<typename T>
    void operator+(T &ptr)
    {
    Log(ptr, DeAllocMem);
    delete ptr;
    ptr = NULL;
    }

    The only problems I've seen with this is with VC++ in which I still
    get compile errors for something like the following:
    delete this;
    Which gives me the following error:
    error C2667: '-' : none of 2 overload have a best conversion
    error C2593: 'operator -' is ambiguous

    However, I don't get this error with the GNU compiler, so I'm thinking
    this is a bug in VC++.
    In any case it's rare to see this type of code, and it can easily be
    modified to the following:
    foofoo* t = this;
    delete t;
    Which makes VC++ happy.


    fraught with perils suggest to me many problems. Is there something
    else you see wrong with the code?

    And I still think there's a C++ Guru out there who can think of a
    better work-around for delete[] operator then what I have.
    foo, Jun 23, 2003
    #3
  4. foo

    markmark

    Joined:
    Aug 25, 2006
    Messages:
    1
    I thought about it for 2 days, then i got the solution using an "if" expression:
    Code:
    char *del_file; //file name
    int del_line;   //line
    
    bool set_del_position(char *file, int line)
    {
     del_file = file;
     del_line = line;
     return true;
    }
    
    #define delete if(set_del_position(__FILE__, __LINE__)) delete
    
    The "if" expression makes sure the block structure is kept correctly.

    The "delete[]" operator with the pointer can be traced:
    Code:
    //code replacement
    inline void operator delete[] (void *p) 
    { free(p); ... }
    
    or
    Code:
    //overloading standart function
    void operator delete[] (void *p) 
    { free(p); ... }
    
    markmark, Aug 25, 2006
    #4
    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. Dead RAM
    Replies:
    20
    Views:
    1,111
    John Harrison
    Jul 14, 2004
  2. Amy
    Replies:
    13
    Views:
    6,013
    Kurt Stutsman
    Feb 23, 2005
  3. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    578
    Jack Klein
    Sep 21, 2003
  4. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    501
    Mark McIntyre
    Nov 22, 2005
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    470
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page