I'm trying to use vectors in a resource management system I am making for a game and keep getting unhandled exceptions during runtime when I try to add elements to the vector, and can't seem to figure out why.
Here is all the code in which the vectors are giving me problems.
Class Definition (vector is initialized):
Code:
class MeshContainer {
private:
vector<Mesh *> MeshList;
public:
MeshContainer() {}
~MeshContainer() {
if(MeshList.size() > 0) {
for(DWORD i=0; i<MeshList.size(); i++) {
if(MeshList[i] != NULL) {
MeshList[i]->MeshData->Release();
MeshList[i] = NULL;
}
}
MeshList.clear();
}
}
void DrawMesh(DWORD dwMeshID);
int LoadMesh(IDirect3DDevice9* &g_pd3dDevice, int iMeshNum);
};
Here is the Mesh structure for reference:
Code:
struct Mesh {
DWORD dwID;
DWORD dwNumSubsets;
LPD3DXMESH MeshData;
Mesh() {
dwID = 0;
dwNumSubsets = 0;
MeshData = NULL;
}
};
And finally, here is the function in which I try to add items to the vector:
Code:
int MeshContainer::LoadMesh(IDirect3DDevice9* &g_pd3dDevice, int iMeshNum) {
Mesh *TempMesh = new Mesh();
switch(iMeshNum) {
case MESH_TEAPOT: // Create a Teapot
if(FAILED(D3DXCreateTeapot(g_pd3dDevice, &TempMesh->MeshData, 0)))
return false;
TempMesh->dwNumSubsets = 0;
TempMesh->dwID = 0;
break;
default:
return false;
break;
}
MeshList.push_back(TempMesh);
return true;
}
I have performed tests and the MeshList vector is being initialized to something other than NULL, it just doesn't seem to be something that I can add items to.
Using the MSVC++ 2005 EE debugger I am pointed to the size() function in the vector class. I am told that this fails when the push_back() function is called (I looked into it and push_back calls size).
Here is the vector size function in case it provides any help:
Code:
size_type size() const
{ // return length of sequence
return (_Myfirst == 0 ? 0 : _Mylast - _Myfirst);
}
The debugger tells me that the return statement is causing the exception (of course, since that is the only statement).
When I try to use reserve() in an attempt to make space so that the vector isn't completely empty, the capacity() function, which is called in reserve, also creates an exception. If necessary I can provide more details about exactly what is going on in that instance however it is almost the exact problem the size() function is having.
Can anyone figure out how this is happening? I am initializing objects of this class elsewhere and calling the functions the right way. Oh and I am include the correct header and I am using namespace std which is why I'm not using std::. The entire thing compiles fine.
Thanks for help in advance.