Thread: Vector Problem

  1. #1
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109

    Vector Problem

    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.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    &TempMesh->MeshData
    That looks wrong to me. Try this program out:
    Code:
    #include <iostream>
    using namespace std;
    
    class Test
    {
    public:
    	Test()
    	{
    		num = 10;
    	}
    
    	int num;
    };
    
    int main()
    {
    	Test t;
    	Test* ptr = &t;
    	cout<<ptr->num<<endl;
    
    	cout<<&ptr<<endl;
    	cout<<&t<<endl;
    	cout<<&ptr->num<<endl;
    
    	return 0;
    }
    Is that what you want?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't see anything wrong with your vector usage in there other than memory leaks. I would check to make sure the MeshContainer that LoadMesh is called on is valid.

  4. #4
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    ARGH!

    All this time I thought the problem was in the MeshContainer class, but this entire thing was because a wrapper class MeshContainer is in was initialized as a pointer and I didn't give it any data. And here I was about to give up on vectors... I am really stupid.

    Alas, it seems I have wasted your time. Sorry . Says a whole lot about my debugging skills.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    No worries. We've all had our *duh* moments.

Popular pages Recent additions subscribe to a feed