memory manager to prevent memory leaks

Discussion in 'C++' started by hbdevelop1@gmail.com, Jan 17, 2014.

  1. Guest

    Hello,

    I have written a memory tracker to prevent memory leaks in my programs.
    The memory tracker meets the following requirements:
    r1-Allocate memory using the system's new/new[].
    r2-Deallocate memory using the system's delete/delete[].
    r3-Log the file name and line where an allocation happens.
    r4-When a memory is fred, remove the memory allocation from the log.
    r5-At application exit, display the log.

    Please find the code at the end of the email.
    I have the following questions :

    1-
    g++ issues the following warning in my operator delete/delete[]: memtracker4.cpp:24:9: warning: deleting ‘void*’ is undefined [enabled by default]
    delete p;
    ^
    Do you have any idea how to correct the code so I don't get the warning ?

    2-
    To remove tracks of memory added in my operator new/new[], I am using template functions deleteArray and deleteObj.
    But I remember in one company I worked in, they were using new(__FILE__,__LINE__) or new[](__FILE__,__LINE__) for allocations and plain delete/delete[] for deallocation.
    And I wonder now, how they were removing memory tracks added by their operator new/new[] since they were using plain delete or delete[] for deallocation.
    Could anybody please tell me if this is possible ? or were they not removing tracks ?

    3-
    Please tell me anything else to improve this code.

    /////// code ////////

    #include <stdio.h>
    #include <new>

    void AddToLog(long ptr, size_t size,const char * filename, int line)
    {
    printf("allocation : 0x%08X,%d,%s,%d\n",ptr, size,filename, line);
    }

    void RemoveFromLog(long ptr)
    {
    printf("deallocation : 0x%08X\n",ptr);
    }

    void * operator new(size_t size, const char *filename, int line)
    {
    void *ptr = ::eek:perator new(size);
    AddToLog((long)ptr, size, filename, line);
    return(ptr);
    };

    void * operator new[](size_t size, const char *filename, int line)
    {
    void *ptr = ::eek:perator new[](size);
    AddToLog((long)ptr, size, filename, line);
    return(ptr);
    };

    void operator delete(void *p, const char *filename, int line)
    {
    RemoveFromLog((long)p);
    delete p; //g++ outputs warning: deleting ‘void*’ is undefined [enabled by default]
    };

    void operator delete[](void *p, const char *filename, int line)
    {
    RemoveFromLog((long)p);
    delete [] p; //g++ outputs warning: deleting ‘void*’ is undefined [enabled by default]
    };


    template<class T> void deleteObj(T *p)
    {
    RemoveFromLog((long)p);
    delete p;
    };

    template<class T> void deleteArray(T *p)
    {
    RemoveFromLog((long)p - sizeof(unsigned int));
    delete [] p;
    };


    struct O11
    {
    int x;
    public:
    ~O11 (){}
    };

    #define new new(__FILE__,__LINE__)

    int main()
    {
    char *c=new char;
    deleteObj<char>(c);

    O11 *o=new O11;
    deleteObj<O11>(o);

    O11 *o1=new O11[10];
    deleteArray<O11>(o1);

    char *c3=new char[3];
    deleteObj<char>(c3);
    /*Note that I am treating c3 as a simple object not as array
    (which caused my question at http://stackoverflow.com/questions/21178252/c-book-detailing-delete-and-delete)
    */

    return 0;
    }
    , Jan 17, 2014
    #1
    1. Advertising

  2. Paavo Helde wrote

    > However, in C++ actually *preventing* memory leaks is usually simple,
    > just don't ever use new/delete (or malloc/free) in the application code.
    > Use automated classes like std::vector instead. If really needed,
    > encapsulate new/delete in a specific RAII class.


    How does Boost implement dynamic memory allocation?
    Nick Baumbach, Jan 18, 2014
    #2
    1. Advertising

  3. Jorgen Grahn Guest

    On Sat, 2014-01-18, Paavo Helde wrote:
    > wrote in
    > news::
    >
    >> Hello,
    >>
    >> I have written a memory tracker to prevent memory leaks in my
    >> programs.

    >
    > This kind of memory tracker does not prevent leaks; at best it just
    > reports them, and *only in the executed code paths* which happen to be
    > passed during the run.

    ....
    (reordered a bit)
    > So, as this memory tracker is both unreliable, subfunctional and most
    > often not needed, I would advise to ditch it and ...


    That's a bit harsh ...

    > However, in C++ actually *preventing* memory leaks is usually simple,
    > just don't ever use new/delete (or malloc/free) in the application code.
    > Use automated classes like std::vector instead. If really needed,
    > encapsulate new/delete in a specific RAII class.


    I agree to that, and I personally have had no or very few resource
    leaks in my C++ code in recent years.

    But I'm always suspicious about advice which assumes:
    - you work only with people who produce good C++ code
    - you do so yourself
    - you always have spare time to go back and rewrite code
    which turned out bad.

    There's nothing wrong with tools which help you deal with imperfect
    code. C++ itself is such a tool!

    My advise would be slightly different:
    - There are many tools which do this already; use some of
    those. (The ones which work with C's malloc/free should work
    for C++ code too.)
    - If you use them and find leaks, think about how you could
    have used the language to make them impossible or unlikely.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Jan 18, 2014
    #3
  4. Guest

    On Saturday, January 18, 2014 2:34:07 AM UTC-5, Paavo Helde wrote:


    >
    > Why would you need __FILE__ and __LINE__ of the point of deletion if you
    > will remove the allocation entry anyway?
    >

    This is because the compiler asks for a matching operator delete for each operator new I overload.

    Thank you very much for your constructive reaction
    , Jan 18, 2014
    #4
  5. Guest

    Thank you very much for your constructive reaction Jorgen Grahn
    , Jan 18, 2014
    #5
    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. ASP.Confused

    ASP.NET - Detecting memory leaks

    ASP.Confused, Jul 16, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,726
    Marina
    Jul 16, 2004
  2. =?Utf-8?B?RnJhbmsxMjEz?=

    Memory Leaks in ASP.NET

    =?Utf-8?B?RnJhbmsxMjEz?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    12
    Views:
    13,816
  3. www
    Replies:
    4
    Views:
    2,099
    Eric Sosman
    Apr 19, 2007
  4. padma
    Replies:
    0
    Views:
    335
    padma
    Oct 3, 2007
  5. Metalone
    Replies:
    0
    Views:
    332
    Metalone
    Jan 6, 2010
Loading...

Share This Page