Jump to content
  • Advertisement
Sign in to follow this  

trouble releasing sprites

This topic is 3315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 {
		LPD3DXSPRITE sprite;

		SPRITE() {
			if (SUCCEEDED(D3DXCreateSprite(_dev, &sprite))) {
		void LoadTexture(const char* Path) {
			D3DXCreateTextureFromFile(_dev, Path, &tex);
		void Draw(VECTOR Position, VECTOR Center, COLOR Color) {
			sprite->Draw(tex, NULL, &D3DXVECTOR3(Center.x, Center.y, 0),
				&D3DXVECTOR3(Position.x, Position.y, 0),
				D3DCOLOR_XRGB(Color.R, Color.G, Color.B));
			_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();
		for (unsigned int x=0; x<_sprs.size(); x++) {
The code compiles and runs, but at the end, when I call this function, I get this error:
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
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
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!