Can you tell the compiler suit you are using?
Removing or avoiding memory leak is tricky. You can use the smart pointers for this. But please be aware, to use smart pointer you have to use C++ compiler and also the ownership issue.
Tips:
1)Ownership issue is important. So read the smart pointer manual carefully and ask ask question you have out here. But try to use a standard/tested smart pointer. (Recently I saw some memory leak due to a custom smart pointer we were using). Be careful about auto_ptr also. As far as I remember it has gone 3 revisions. So be careful.
2)Don't ignore small leaks. for example:
void foo()
{
int* arr = new int[500];
}
void bar()
{
int* arr = new int;
}
void caller()
{
foo();
for(int i = 0; i < 10000; ++i)
bar();
}
Here as you can see function
bar()
is called repeatedly, more than
foo()
. So although
bar()
leaks little on its own, it leaks a huge when called repeatedly.
3) Sometime tools show some so called leaks, but in reality they may not be leaks. What happens is, some tools use a conservative garbage collector algorithm to detect leaks. Which appear to be a leak at one point of time, may not remain a leak when the program ends.
Few articles and tools that you will find good are:
1. libumem is good for solaris. Recently its ported to linux and windows too.
2. Valgrid is also useful for linux environments.
3.
http://msdn.microsoft.com/en-us/library/x98tx3cf%28v=vs.80%29.aspx[
^] . This is an article for windows visual studio users.
4.
http://www.linuxjournal.com/article/6556[
^] . Nice introduction.
5.
http://www.linkdata.se/sourcecode/memwatch/[
^]
6.
Memory Leak Detection[
^]
7.
http://wyw.dcweb.cn/leakage.htm[
^] . Nice article, if you go through it carefully, you can roll you own memory debugger also. ;)