Pretty much boils down to pairing a delete with every new. Whenever you dynamically allocate memory in your program YOU are responsible for freeing that allocated memory. Otherwise, the memory will always be flagged as in use and if you keeping using new without delete, more and more memory will become unusable.. hence a memory leak.
example: Two classes, Player and Item. Player class contains an Item pointer for its weapon.
class Player
{
public:
Player() : weapon(new Item) { } // Default constructor. Create player and make a new weapon
private:
Item* weapon;
};
So you see here, we have a new Player that has dynamically allocated memory for a weapon. Say we have 500 players online, each object has dynamically allocated memory for their weapons.
Now a player logs off, the player object is gone. But we have a problem, the memory used for the Item is still 'in use' because we never freed that memory. So player will keep logging in and out and in and out tieing up more memory!
We need to fix that with a delete in our destructor.
~Player() { delete item; } // Frees the memory from weapon when player object is destroyed.
Problem solved.
A note for dynamically allocated arrays.. say you have:
// Dynamic array! User creates size.
int n;
std::cout << "Enter an array size: ";
std::cin >> n;
int* x = new int[n]; // Dynamically allocated array of size n.
The syntax for deleting arrays is a little different:
delete [] x; // Brackets are used to delete dynamically allocated arrays.
So just remember, everytime you use the 'new' keyword.. there better be a 'delete' for it somewhere!
Hope this helps.