[closed] Problem with writing a small memory leak log (very basic)
hi!
i wanted to write a little memory logger on my own...
to achieve my goal i overloaded the new, new[], delete amd
delete[] operators...
i want to log the lines where i allocate memory and the filename...
so i created a macro...
for new it looks like this...
void* operator new(size_t size, const char* file, int line) {
cout<<endl<<"File: "<<file<<endl<<"Line: "<<line<<endl;
return malloc(size);
}
in the main.cpp i define the makro:
#define new new(__FILE__,__LINE__)
i wanted to make this for the delete operator in the
similar way... but i can´t get a working makro for the
delete[] operator...
the following works just for the "normal" delete operator
inline void operator delete(void* mem) {
cout<<"global delete"<<endl;
free(mem);
}
inline void getInfo(const char* file, int line) {
cout<<endl<<"Filename: "<<file<<"Line: "<<line<<endl;
}
in the main.cpp:
#define delete(A) { delete(A); getInfo(__FILE__, __LINE__);}
this dows only work for the delte operator but not for the
delete[]... (i may not use [] cause the preprocessor doesn´t want
to handle that.
thx for your help
John
PS: if someone knows another way how to log where memory is allocated
and freed please tell me... it doesn´t have to be THIS way ;)
[Edited by - Noobico on May 30, 2005 2:49:38 PM]
An easier way, instead of trying to do with delete that you do with new, is creating a hash map ( You'd need a new allocator that uses malloc/free ) that stores the ptr, file, and line of said allocation, then search through the hash map for the ptr given to delete, free the ptr, free the struct. ( The reason you'd free the struct is because you'd want to store a pointer to the struct, and you should generally allocate memory using malloc()/free() in overloaded new/delete/new[]/delete[] operators. )
Sorry if I wasn't clear on something.
[edit]
Come to think of it, this way is somewhat inefficient. You'd be adding entries to a hash map every call to new/new[]/delete/delete[]. An easier way is to create a manager class and overload new/delete in those, then you can use global new/delete to allocate the memory and store debug infomation for derived classes.
Sorry if I wasn't clear on something.
[edit]
Come to think of it, this way is somewhat inefficient. You'd be adding entries to a hash map every call to new/new[]/delete/delete[]. An easier way is to create a manager class and overload new/delete in those, then you can use global new/delete to allocate the memory and store debug infomation for derived classes.
i don´t exactly know if i get what you mean.
i don´t know how to get the line in which the
allocation is executed. and i can´t overwrite
the global delete[] so how can i automatically
get the line and the file were the memory is actually
allocated if just the delete[] operator is executed?
and if i make a class and overwrite the operators there
it wouldn´t change the global ones...
well i think i got something wrong...
i hope someone will enlighten me *g*
i don´t know how to get the line in which the
allocation is executed. and i can´t overwrite
the global delete[] so how can i automatically
get the line and the file were the memory is actually
allocated if just the delete[] operator is executed?
and if i make a class and overwrite the operators there
it wouldn´t change the global ones...
well i think i got something wrong...
i hope someone will enlighten me *g*
His problem is that he can't write a macro for delete[] because #define can't use []. He needs a macro to get hold of __FILE__ and __LINE__.
There no way to do that I'm afraid.
You can do something like this though:
#define ARRAY_DELETE...
There no way to do that I'm afraid.
You can do something like this though:
#define ARRAY_DELETE...
i want to code it in a way that older code could be checked
with it too...
is it possible to define a typedef for the delete[] operator
and then pass the typedef name to the preprocessor?
i never used typedef so i don´t know if this is possible..
i hope someone can tell me if that´s possible...
with it too...
is it possible to define a typedef for the delete[] operator
and then pass the typedef name to the preprocessor?
i never used typedef so i don´t know if this is possible..
i hope someone can tell me if that´s possible...
What I did was the following:
Where the method preDelete would store the given file and line in the static member variables, while MemSafeDelete (the method that actually deletes stuff) uses these static variables in case of an error.
Maybe not the most clean way to do it (it requires some effort to make it thread safe), but it works for me.
The trick here is probably that I haven't #define-d delete as a macro function.
class MemSafe { static char *sm_deleteFile; static unsigned long sm_deleteLine; ...};#define MemSafeDelete MemSafe::MemSafe_preDelete(__FILE__, __LINE__), delete#define delete MemSafeDelete
Where the method preDelete would store the given file and line in the static member variables, while MemSafeDelete (the method that actually deletes stuff) uses these static variables in case of an error.
Maybe not the most clean way to do it (it requires some effort to make it thread safe), but it works for me.
The trick here is probably that I haven't #define-d delete as a macro function.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement