Sign in to follow this  
iko

trouble releasing sprites

Recommended Posts

iko    100
I'm trying to make a very basic 2D game engine with DirectX 9 and VC++ 2008 Express. I wanted to make it so that using the engine didn't require me to release all my sprites at the end, so I defined my own sprite class:
std::list<LPD3DXSPRITE> _sprs;

class SPRITE {
	public:
		LPD3DXSPRITE sprite;
		LPDIRECT3DTEXTURE9 tex;

		SPRITE() {
			if (SUCCEEDED(D3DXCreateSprite(_dev, &sprite))) {
				_sprs.push_back(sprite);
			}
		}
		void LoadTexture(const char* Path) {
			D3DXCreateTextureFromFile(_dev, Path, &tex);
		}
		void Draw(VECTOR Position, VECTOR Center, COLOR Color) {
			_dev->BeginScene();
			sprite->Begin(D3DXSPRITE_ALPHABLEND);
			sprite->Draw(tex, NULL, &D3DXVECTOR3(Center.x, Center.y, 0),
				&D3DXVECTOR3(Position.x, Position.y, 0),
				D3DCOLOR_XRGB(Color.R, Color.G, Color.B));
			sprite->End();
			_dev->EndScene();
			_dev->Present(NULL, NULL, NULL, NULL);
		}
};
you'll notice that in the SPRITE constructor I had the LPD3DXSPRITE sprite added to the end of a list called _sprs. This is so that I can store all of the sprites I define in a global variable which I can later use to release them all. at the end of the program, I call my releaseSprites() function:
void releaseSprites() {
	if (_sprs.size()>0) {
		std::list<LPD3DXSPRITE>::iterator itr=_sprs.begin();
		LPD3DXSPRITE spr;
		for (unsigned int x=0; x<_sprs.size(); x++) {
			spr=*itr;
			spr->Release();
			++itr;
		}
	}
}
The code compiles and runs, but at the end, when I call this function, I get this error:
Quote:
Unhandled exception at 0x002f50ad in dx92d.exe: 0xC0000005: Access violation reading location 0x0000000b.
What can I do to fix this?

Share this post


Link to post
Share on other sites
MJP    19754
Why store all of your textures in a global list? I would think it would make more sense to let each SPRITE instance clean up its own garbage. Otherwise it's much easier to run into a situation where a SPRITE is still alive, but its textures have already been released.

Share this post


Link to post
Share on other sites
Pedigree_D    126
Why dont you declare a destructor and have it release the texture and sprites? That way you dont have to have a global list. You add a sprite to the list every time you create a new instance of SPRITE so why not have that same instance clean up its own mess. It doesn't seem like your releasing the texture inside SPRITE or maybe its another function you haven't shown.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this