G
goodfella
I am curious about creating some macro's for keeping track of where
memory has been allocated on the heap. For example it would be nice
to be able to detect memory leaks at the end of program execution. I
would like a system that does not incur to much size and speed
overhead. Here is my design for a way to manage who created what and
where:
#ifndef HEAP_OBJECT_H
#define HEAP_OBJECT_H
struct code_loc
{
code_loc(const char* f, const char* fu, int l):
file(f), func(fu), line(l)
{}
const char* file;
const char* func;
int line;
};
template<class T>
class heap_object
{
public:
heap_object(const T&, const struct code_loc&);
T obj;
struct code_loc loc;
};
template<class T>
heap_object<T>::heap_object(const T& p, const struct code_loc& l):
obj(p), loc(l)
{}
#endif
The macros that implement the system are as follows:
#define track_new(init) (typeof(init)*)new
heap_object<typeof(init)>(init,code_loc(__FILE__,__PRETTY_FUNCTION__,__LINE__))
#define track_delete(pointer) delete
(heap_object<typeof(*pointer)>*)pointer
So basically I have a template class whose first data member is an
object of the type that I want to create. My assumption is that the
offset to this member is zero, so by type casting the pointer to the
heap_object to the type I want; I would have a valid pointer to the
type I want. So my first question is,
"Is it a safe assumption that the first data member of a class always
has an offset of zero?"
I have ran some limited tests with valgrind and it does not seem to
detect memory errors or leaks. I figure as long as I can insure that
track_delete is only called on objects created with track_new the code
is ok. Of course I will need some kind of list to keep track of what
has and has not been deleted, as well as be able to remove objects
from such a list that have been deleted in O(1) time. I think I have
that solved but of course the whole system is dependent on my
assumption above. Any help would be appreciated thanks.
memory has been allocated on the heap. For example it would be nice
to be able to detect memory leaks at the end of program execution. I
would like a system that does not incur to much size and speed
overhead. Here is my design for a way to manage who created what and
where:
#ifndef HEAP_OBJECT_H
#define HEAP_OBJECT_H
struct code_loc
{
code_loc(const char* f, const char* fu, int l):
file(f), func(fu), line(l)
{}
const char* file;
const char* func;
int line;
};
template<class T>
class heap_object
{
public:
heap_object(const T&, const struct code_loc&);
T obj;
struct code_loc loc;
};
template<class T>
heap_object<T>::heap_object(const T& p, const struct code_loc& l):
obj(p), loc(l)
{}
#endif
The macros that implement the system are as follows:
#define track_new(init) (typeof(init)*)new
heap_object<typeof(init)>(init,code_loc(__FILE__,__PRETTY_FUNCTION__,__LINE__))
#define track_delete(pointer) delete
(heap_object<typeof(*pointer)>*)pointer
So basically I have a template class whose first data member is an
object of the type that I want to create. My assumption is that the
offset to this member is zero, so by type casting the pointer to the
heap_object to the type I want; I would have a valid pointer to the
type I want. So my first question is,
"Is it a safe assumption that the first data member of a class always
has an offset of zero?"
I have ran some limited tests with valgrind and it does not seem to
detect memory errors or leaks. I figure as long as I can insure that
track_delete is only called on objects created with track_new the code
is ok. Of course I will need some kind of list to keep track of what
has and has not been deleted, as well as be able to remove objects
from such a list that have been deleted in O(1) time. I think I have
that solved but of course the whole system is dependent on my
assumption above. Any help would be appreciated thanks.