You are on page 1of 24

Memory Leaks

Why They Suck and How to Avoid Them


Dynamic Memory Allocation

using namespace std;

int main()
{
int* foo;

//More code goes here


}
Dynamic Memory Allocation

using namespace std;

int main()
{
int* foo;
foo = new int;

//More code goes here


}
Dynamic Memory Allocation

using namespace std;

int main()
{
int* foo;
foo = new int;
*foo = 3;

//More code goes here


}
Dynamic Memory Allocation

using namespace std;

int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int;
//More code goes here
}
Dynamic Memory Allocation
The Stack

foo
using namespace std;

int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int; The Heap
//More code goes here
}
Dynamic Memory Allocation
The Stack

foo
using namespace std;

int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int; The Heap
//More code goes here
} int
Dynamic Memory Allocation
The Stack

foo
using namespace std;

int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int; The Heap
//More code goes here
} int
3
Dynamic Memory Allocation
The Stack

foo
using namespace std;

int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int; The Heap
//More code goes here
} int int
3
Dynamic Memory Allocation
The Stack

foo
using namespace std;

int main()
{
int* foo;
foo = new int;
*foo = 3;
foo = new int; The Heap
//More code goes here
} int int
3
We have
ABANDONED this
integer!
int
3
We have
ABANDONED this
integer!
int
We have NO WAY to
3
tell the computer to
free up the memory
it resides in!
WE HAVE
LEAKED
THIS
MEMORY
In this example, the amount of memory we
leaked was pretty small (only a few bytes, really)
In this example, the amount of memory we
leaked was pretty small (only a few bytes, really)

But in larger programs running for longer periods


of time, these small memory leaks can add up to
GIGABYTES.
In this example, the amount of memory we
leaked was pretty small (only a few bytes, really).

But in larger programs running for longer periods


of time, these small memory leaks can add up to
GIGABYTES.

So how do we avoid this?


The delete operator!
The delete operator!

When we use the delete operator on a pointer


that points to a dynamically allocated object, the
operating system will deallocate the memory that
object takes up.
How to Use the delete Operator
The Stack

foo
using namespace std;

int main()
{
int* foo;
foo = new int;
*foo = 3;
The Heap
foo = new int;
//More code goes here int
} 3
How to Use the delete Operator
The Stack

foo
using namespace std;

int main()
{
int* foo;
foo = new int;
*foo = 3;
delete foo; The Heap
foo = new int;
//More code goes here
}
How to Use the delete Operator
The Stack

foo
using namespace std;

int main()
{
int* foo;
foo = new int;
*foo = 3;
delete foo; The Heap
foo = new int;
//More code goes here int
}
How to Use the delete Operator
The Stack

foo
using namespace std;

int main()
{
int* foo;
foo = new int;
*foo = 3;
delete foo; The Heap
foo = new int;
//More code goes here int
}

No more leaked memory!


Some Notes on the delete Operator
When you want to delete a dynamically allocated array, you have to
use delete[]
Otherwise, you just delete the first element of the array.
If you try to delete an object on the stack, your program will crash
When you use delete, it has to be on a pointer to the object you
want to delete, not the object itself
i.e. delete *foo would not work unless foo pointed to a pointer
A Good Axiom to Remember:

For every act of new, there


must be at least one act of
delete

You might also like