Your loop is incorrect. It allows invalidated iterators to be accessed. It should be:
void SpriteUnload(string szNameOrType)
// Erase any valid items from vector array
std::vector<Asset>::iterator it = VectorAsset.begin();
while(it != VectorAsset.end())
if(szNameOrType==it->szName || szNameOrType==it->szType || szNameOrType=="ALL")
it = VectorAsset.erase(it);
Note the assignment to "it" of the return value of "erase()".
The "class" keyword here is superfluous. It doesn't change anything. Putting the variable's name in parenthesis is highly unusual and degrades readability.
So, when I call VectorAsset.erase(it); does the destructor get called still?
Some destructor will get called, yes. That is necessary, but not sufficient, for ensuring you are not leaking. The real question I suspect you want to know the answer to is whether you are leaking memory.
As I have alot of DirectX code that needs to be cleaned up via the destructor (in each instance).
Are you correctly implementing a copy constructor? If not, you cannot store your objects in the vector by value. As the others have mentioned, you almost certainly want to store (smart) pointers to your objects in the vector.
Testing my destructor, it is actually getting called when the class is created.
When you are copying the object into the vector, a temporary is created. This temporary is then destroyed. If you log your constructor, copy constructor, assignment operator and destructor you'll get a better picture of the lifecycle of various instances (particularly if you print the value of "this" in each log statement).
So, should I ignore the destructors and call a function within the class to handle the cleanup instead?
No, you need to understand C++ value and reference semantics, and choose the appropriate one. If you choose value semantics, you must also correctly implement the rule of three
Edited by rip-off, 31 July 2012 - 03:03 AM.