Thread: Mesh Release() Problem

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

    Thumbs down Mesh Release() Problem

    I come from one problem onto the next one very quickly it seems.

    I have never had a problem like this one before, and I don't think the reason is as stupid as the last one (though I could definitely be wrong).

    Anyways, when I try to release stored mesh data in my program I am given an unhandled exception (again). The debugger tells me it roots back to IUnknown, specifically I get this error:
    __vfptr CXX0030: Error: expression cannot be evaluated
    Here is the code for the class where the destructor is being called in which this error is generated:
    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]->MeshData != NULL) {
    					MeshList[i]->MeshData->Release();
    					MeshList[i]->MeshData = NULL;
    				}
    				delete MeshList[i];
    			}
    			MeshList.clear();
    		}
    	}
    	int DrawMesh(DWORD dwMeshID);
    	int LoadMesh(IDirect3DDevice9* &g_pd3dDevice, int iMeshNum);
    };
    I am getting the error from the line:
    Code:
    MeshList[i]->MeshData->Release();
    When I reach there, the program crashes.

    Here is the mesh structure for reference:
    Code:
    struct Mesh {
    	DWORD dwID;
    	DWORD dwNumSubsets;
    	ID3DXMesh* MeshData;
    	Mesh() {
    		dwID = 0;
    		dwNumSubsets = 0;
    		MeshData = NULL;
    	}
    };
    And since I don't want to give to little like last time...
    Here is the wrapper class which the MeshContainer class is used in:
    Code:
    class ResourceCache {
    private:
    	friend class CObjectManager;
    	MaterialContainer *Materials;
    	TextureContainer *Textures;
    	MeshContainer *Meshes;
    public:
    	ResourceCache() {
    		Materials = new MaterialContainer();
    		Textures = new TextureContainer();
    		Meshes = new MeshContainer();
    	}
    	~ResourceCache() {
    		delete Materials;
    		delete Textures;
    		delete Meshes;
    	}
    	int DrawMesh(DWORD dwMeshID) {
    		return Meshes->DrawMesh(dwMeshID);
    	}
    	int CreateMesh(LPDIRECT3DDEVICE9 &g_pd3dDevice, int iMeshNum) { return Meshes->LoadMesh(g_pd3dDevice, iMeshNum); }
    };
    Here is the initialization of that wrapper class elsewhere:
    Code:
    ResourceCache*							ResourceList = new ResourceCache();
    And finally, here is the deletion statement for that wrapper class object:
    Code:
    delete ResourceList;
    Here is the function in which I create the mesh data:
    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 am using MSVC++ 2005 EE (still). I'm not sure what the best way to explain the problem is. All I know is, I can't access the MeshData->Release() function successfully. This is a pain and it shouldn't be. I'm stumped (again). If something needs further clarification, just ask and I'll try to explain better.

    Thanks in advance... .
    Sic vis pacum para bellum. If you want peace, prepare for war.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The only problem could be that MeshData is NULL. You would be attempting to call release on a NULL pointer. Instant crash.

  3. #3
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    That's what I first thought but I ran some tests and I have no reason to believe that the
    Code:
    if (MeshList[i]->MeshData != NULL) {...}
    would be failing to catch MeshData as NULL.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Hmmm. Very odd. I don't see anything that would auto-decrement the reference count for ID3DXMesh.

  5. #5
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    The only other function that was calling on the mesh data is/was the draw function (which isn't completed, I'm going to eventually take out the whole teapot thing and just have the LoadMesh function load from files):

    Code:
    int MeshContainer::DrawMesh(DWORD dwMeshID) {
    	if((MeshList.size() > 0) && (dwMeshID < MeshList.size()))
    		if(MeshList[dwMeshID])
    			for(DWORD i=0; i<MeshList[dwMeshID]->dwNumSubsets; i++)
    				if(!MeshList[dwMeshID]->MeshData->DrawSubset(i))
    					return false;
    	return true;
    }
    Now, I didn't think this could possible by effecting anything. But, sure enough, when I went and commented out the line that called this code (indirectly), the problem stopped. This is interesting. The problem seems to be intertwined with my CObjectManager class. When I did comment out that line, I got an assert failure in my object list destructor. This should not be happening, since I am initializing the objects on creation and not during the rendering loop (I'm calling DrawMesh through draw functions in my objects).

    I'm going to run some more tests and I'll post my results.
    Sic vis pacum para bellum. If you want peace, prepare for war.

  6. #6
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    something that i see is that when you
    Code:
    delete MeshList[i]
    what is happening is you are deleting the memory there and the spot in you vector, so every thing will be moved up in you vector. so you need to do
    Code:
    delete MeshList[i]
    i--;
    this is only a issue because you used .size() for your comparison

    i dont know if this will fix anything but it is some thing to look at

  7. #7
    Yah. Morgul's Avatar
    Join Date
    Feb 2005
    Posts
    109
    I've been busy for a while and haven't had a chance to completely go over this. However, I took lot's of the stuff apart looking for the error, didn't find anything, then put everything back together exactly the way it was and it started working all of a sudden. I have no idea what happened, but I guess that is ok since it is fixed. Thanks for the help everyone.
    Sic vis pacum para bellum. If you want peace, prepare for war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Vector Problem
    By Morgul in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2006, 12:36 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM