Question: I'm using a vector of pointers to objects. These objects are derived from a base class, and are being dynamically allocated and stored.

For example, I have something like:

```
vector<Enemy*> Enemies;

```

and I'll be deriving from the Enemy class and then dynamically allocating memory for the derived class, like this:

```
enemies.push_back(new Monster());

```

What are things I need to be aware of to avoid memory leaks and other problems?

Answer: 
The trouble with using `vector<T*>` is that, whenever the vector goes out of scope unexpectedly (like when an exception is thrown), the vector cleans up after yourself, but this will only free the memory it manages for holding the *pointer*, not the memory you allocated for what the pointers are referring to. So [GMan's `delete_pointed_to` function](https://stackoverflow.com/questions/1361139/c-vector-pointers-to-objects/1361227#1361227) is of limited value, as it only works when nothing goes wrong. 

What you need to do is to use a smart pointer: 

```
vector< std::tr1::shared_ptr<Enemy> > Enemies;

```

(If your std lib comes without TR1, use `boost::shared_ptr` instead.)
Except for very rare corner cases (circular references) this simply removes the trouble of object lifetime. 

*Edit*: Note that GMan, in his detailed answer, mentions this, too.
One thing to be very careful is IF there are two Monster() DERIVED objects whose contents are identical in value. Suppose that you wanted to remove the DUPLICATE Monster objects from your vector (BASE class pointers to DERIVED Monster objects). If you used the standard idiom for removing duplicates (sort, unique, erase: see LINK #2], you will run into memory leak issues, and/or duplicate delete problems, possibly leading to SEGMENTATION VOIOLATIONS (I have personally seen these problems on LINUX machine).

THe problem with the std::unique() is that the duplicates in the [duplicatePosition,end) range [inclusive, exclusive) at the end of the vector are undefined as ?. What can happen is that those undefined ((?) items might be extra duplicate or a missing duplicate.

The problem is that std::unique() isn't geared to handle a vector of pointers properly. The reason is that std::unique copies uniques from the end of the vector "down" toward the beginning of the vector. For a vector of plain objects this invokes the COPY CTOR, and if the COPY CTOR is written properly, there is no problem of memory leaks. But when its a vector of pointers, there is no COPY CTOR other than "bitwise copy", and so the pointer itself is simply copied.

THere are ways to solve these memory leak other than using a smart pointer. One way to write your own slightly modified version of std::unique() as "your\_company::unique()". The basic trick is that instead of copying an element, you would swap two elements. And you would have to be sure that the instead of comparing two pointers, you call BinaryPredicate that follows the two pointers to the object themselves, and compare the contents of those two "Monster" derived objects.

1) @SEE\_ALSO: <http://www.cplusplus.com/reference/algorithm/unique/>

2) @SEE\_ALSO: [What's the most efficient way to erase duplicates and sort a vector?](https://stackoverflow.com/questions/1041620/most-efficient-way-to-erase-duplicates-and-sort-a-c-vector)

2nd link is excellently written, and will work for a std::vector but has memory leaks, duplicate frees (sometimes resulting in SEGMENTATION violations) for a std::vector

3) @SEE\_ALSO: valgrind(1). THis "memory leak" tool on LINUX is amazing in what it can find! I HIGHLY recommend using it!

I hope to post a nice version of "my\_company::unique()" in a future post. Right now, its not perfect, because I want the 3-arg version having BinaryPredicate to work seamlessly for either a function pointer or a FUNCTOR, and I'm having some problems handling both properly. IF I cannot solve those problems, I'll post what I have, and let the community have a go at improving on what I have done so far.