I started work on a pong game just to get back into game programming since it's been a while for me. Everything was working fine until I came about my current problem. I have an Entity Manager class that handles iterating through a list of entities within the game (paddles and ball) and rendering them. The program runs fine, however when I end it an Unhandled Exception pops up concerning the Release() of the texture the paddle has. The interesting thing is that if I run the program without the list and render the paddle manually, it works fine without the error. Hopefully someone knows what I'm doing wrong? Also after seeing the code if you have any suggestions on how I can make something cleaner or more efficient I would love to hear it
EntityManager.h
#ifndef ENTITYMANAGER
#define ENTITYMANAGER
#include "Globals.h"
#include "Entity.h"
#include <list>
class EntityManager
{
public:
void Add( Entity &entity );
void Update( LPD3DXSPRITE sprite );
private:
std::list<Entity> entities;
std::list<Entity>::iterator iter;
};
#endif ENTITYMANAGER
EntityManager.cpp
#include "EntityManager.h"
void EntityManager::Add( Entity &entity )
{
entities.push_back( entity );
}
void EntityManager::Update( LPD3DXSPRITE sprite )
{
iter = entities.begin();
while ( iter != entities.end() )
{
sprite -> Draw( iter -> GetTexture(), NULL, NULL, iter -> GetPosition(), White );
iter++;
}
}
Game.h
#ifndef GAME_H
#define GAME_H
#include "Globals.h"
#include "DxManager.h"
#include "Entity.h"
#include "EntityManager.h"
class Game
{
public:
bool Init( HWND &window );
void Run();
private:
HWND window;
DxManager GManager;
Entity LeftPaddle;
//Entity RightPaddle;
//Entity Ball;
EntityManager EManager;
};
#endif GAME_H
Game.cpp
#include "Game.h"
bool Game::Init( HWND &window )
{
this -> window = window;
if ( !GManager.InitD3D( this -> window ) )
{
GameOver = true;
return false;
}
LeftPaddle.LoadTexture( GManager.GetDevice(), "Resources/LeftPaddle.png" );
EManager.Add( LeftPaddle );
return true;
}
void Game::Run()
{
GManager.Begin();
EManager.Update( GManager.GetSprite() );
//GManager.GetSprite() -> Draw( LeftPaddle.GetTexture(), NULL, NULL, LeftPaddle.GetPosition(), White ); * program runs fine if i use this line instead of EManager.Update( GManager.GetSprite() ); *
GManager.End();
}
Error
First-chance exception at 0x000F14AA in Pong.exe: 0xC0000005: Access violation reading location 0xFEEEFEF6.
Unhandled exception at 0x000F14AA in Pong.exe: 0xC0000005: Access violation reading location 0xFEEEFEF6.