memory manager to prevent memory leaks

H

hbdevelop1

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;
}
 
N

Nick Baumbach

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?
 
J

Jorgen Grahn

(e-mail address removed) wrote in


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
 
H

hbdevelop1

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top