boost smart pointers are good, but deleting memory is possible on std::vectors as well, and this is how I do it:
Code:
#include <algorithm> //Otherwise anything that uses these has to manually include this
template<class T>
struct VectorDeleter
{
void operator()(T * & obj) //reference to a pointer, not a pointer to a reference
{
if(obj)
{
delete obj;
obj = NULL; //I don't know if this actually does anything...hmm
}
}
};
//Deleter defined in GAMEEXPORT.H right now
#define DELETE_MEMORY_AND_ERASE(a,t) std::for_each(a.begin(),a.end(),VectorDeleter<t>()); a.clear();
#define DELETE_INDEXED_MEMORY_AND_ERASE(vec,type,ptr) std::for_each(ptr,ptr+1,VectorDeleter<type>()); vec.clear();
The algorithm header file is for std::for_each
the first macro deletes all of the dynamic memory on the vecotr, the second only deletes the memory at one index into the vector (so you can delete the i'th value in the array).
It's not immediately clear to understand, and I had some help generating this code so it's not like I 100% wrote this on my own.