Better cleanup for simulation - fix memory leaks

This commit is contained in:
Simon Robertshaw 2012-01-21 13:19:10 +00:00
parent d364a27ed6
commit 8ec6aae617
17 changed files with 80 additions and 8 deletions

View File

@ -2366,3 +2366,7 @@ Graphics::Graphics()
{ {
vid = (pixel *)malloc(PIXELSIZE * ((XRES+BARSIZE) * (YRES+MENUSIZE))); vid = (pixel *)malloc(PIXELSIZE * ((XRES+BARSIZE) * (YRES+MENUSIZE)));
} }
Graphics::~Graphics()
{
free(vid);
}

View File

@ -167,6 +167,7 @@ public:
void ogl_blit(int x, int y, int w, int h, pixel *src, int pitch, int scale); void ogl_blit(int x, int y, int w, int h, pixel *src, int pitch, int scale);
#endif #endif
Graphics(); Graphics();
~Graphics();
}; };
#endif #endif

View File

@ -124,4 +124,7 @@ int main(int argc, char * argv[])
delta = 60.0f/fps; delta = 60.0f/fps;
} }
} }
ui::Engine::Ref().CloseWindow();
delete gameController;
delete ui::Engine::Ref().g;
} }

View File

@ -1697,3 +1697,12 @@ Renderer::Renderer(Graphics * g, Simulation * sim):
flm_data = Graphics::generate_gradient(flm_data_colours, flm_data_pos, flm_data_points, 200); flm_data = Graphics::generate_gradient(flm_data_colours, flm_data_pos, flm_data_points, 200);
plasma_data = Graphics::generate_gradient(plasma_data_colours, plasma_data_pos, plasma_data_points, 200); plasma_data = Graphics::generate_gradient(plasma_data_colours, plasma_data_pos, plasma_data_points, 200);
} }
Renderer::~Renderer()
{
free(graphicscache);
free(flm_data);
free(plasma_data);
free(render_modes);
free(display_modes);
}

View File

@ -55,6 +55,7 @@ public:
void get_sign_pos(int i, int *x0, int *y0, int *w, int *h); void get_sign_pos(int i, int *x0, int *y0, int *w, int *h);
void prepare_graphicscache(); void prepare_graphicscache();
Renderer(Graphics * g, Simulation * sim); Renderer(Graphics * g, Simulation * sim);
~Renderer();
}; };
#endif #endif

View File

@ -35,6 +35,7 @@ Client::Client()
Client::~Client() Client::~Client()
{ {
ClearThumbnailRequests();
http_done(); http_done();
} }

View File

@ -17,7 +17,13 @@ GameController::GameController()
gameView->AttachController(this); gameView->AttachController(this);
gameModel->AddObserver(gameView); gameModel->AddObserver(gameView);
sim = new Simulation(); //sim = new Simulation();
}
GameController::~GameController()
{
delete gameView;
delete gameModel;
} }
GameView * GameController::GetView() GameView * GameController::GetView()

View File

@ -14,11 +14,12 @@ class GameView;
class GameController class GameController
{ {
private: private:
Simulation * sim; //Simulation * sim;
GameView * gameView; GameView * gameView;
GameModel * gameModel; GameModel * gameModel;
public: public:
GameController(); GameController();
~GameController();
GameView * GetView(); GameView * GetView();
void DrawPoints(queue<ui::Point*> & pointQueue); void DrawPoints(queue<ui::Point*> & pointQueue);
void Tick(); void Tick();

View File

@ -13,6 +13,12 @@ GameModel::GameModel():
ren = new Renderer(ui::Engine::Ref().g, sim); ren = new Renderer(ui::Engine::Ref().g, sim);
} }
GameModel::~GameModel()
{
delete sim;
delete ren;
}
void GameModel::AddObserver(GameView * observer){ void GameModel::AddObserver(GameView * observer){
observers.push_back(observer); observers.push_back(observer);

View File

@ -23,7 +23,8 @@ private:
void notifySimulationChanged(); void notifySimulationChanged();
void notifyPausedChanged(); void notifyPausedChanged();
public: public:
GameModel(); GameModel();
~GameModel();
void AddObserver(GameView * observer); void AddObserver(GameView * observer);
int GetActiveElement(); int GetActiveElement();
void SetActiveElement(int element); void SetActiveElement(int element);

View File

@ -27,6 +27,12 @@ Engine::~Engine()
{ {
if(state_ != NULL) if(state_ != NULL)
delete state_; delete state_;
//Dispose of any Windows.
while(!windows.empty())
{
delete windows.top();
windows.pop();
}
} }
void Engine::Begin(int width, int height) void Engine::Begin(int width, int height)

View File

@ -17,6 +17,12 @@ SearchController::SearchController()
//windowPanel.AddChild(); //windowPanel.AddChild();
} }
SearchController::~SearchController()
{
delete searchModel;
delete searchView;
}
void SearchController::DoSearch(std::string query) void SearchController::DoSearch(std::string query)
{ {
searchModel->UpdateSaveList(query); searchModel->UpdateSaveList(query);

View File

@ -13,6 +13,7 @@ private:
SearchView * searchView; SearchView * searchView;
public: public:
SearchController(); SearchController();
~SearchController();
SearchView * GetView() { return searchView; } SearchView * GetView() { return searchView; }
void DoSearch(std::string query); void DoSearch(std::string query);
}; };

View File

@ -48,6 +48,9 @@ void Gravity::gravity_init()
gravx = (float *)calloc((XRES/CELL)*(YRES/CELL), sizeof(float)); gravx = (float *)calloc((XRES/CELL)*(YRES/CELL), sizeof(float));
gravp = (float *)calloc((XRES/CELL)*(YRES/CELL), sizeof(float)); gravp = (float *)calloc((XRES/CELL)*(YRES/CELL), sizeof(float));
gravmask = (unsigned int *)calloc((XRES/CELL)*(YRES/CELL), sizeof(unsigned)); gravmask = (unsigned int *)calloc((XRES/CELL)*(YRES/CELL), sizeof(unsigned));
#ifdef GRAVFFT
grav_fft_init();
#endif
} }
void Gravity::gravity_cleanup() void Gravity::gravity_cleanup()
@ -55,6 +58,17 @@ void Gravity::gravity_cleanup()
#ifdef GRAVFFT #ifdef GRAVFFT
grav_fft_cleanup(); grav_fft_cleanup();
#endif #endif
//Free gravity info
free(th_ogravmap);
free(th_gravmap);
free(th_gravy);
free(th_gravx);
free(th_gravp);
free(gravmap);
free(gravy);
free(gravx);
free(gravp);
free(gravmask);
} }
void Gravity::gravity_update_async() void Gravity::gravity_update_async()
@ -124,9 +138,6 @@ void Gravity::update_grav_async()
//memset(th_gravy, 0, XRES*YRES*sizeof(float)); //memset(th_gravy, 0, XRES*YRES*sizeof(float));
//memset(th_gravx, 0, XRES*YRES*sizeof(float)); //memset(th_gravx, 0, XRES*YRES*sizeof(float));
//memset(th_gravp, 0, XRES*YRES*sizeof(float)); //memset(th_gravp, 0, XRES*YRES*sizeof(float));
#ifdef GRAVFFT
grav_fft_init();
#endif
while(!thread_done){ while(!thread_done){
if(!done){ if(!done){
update_grav(); update_grav();
@ -486,3 +497,8 @@ Gravity::Gravity()
{ {
gravity_init(); gravity_init();
} }
Gravity::~Gravity()
{
gravity_cleanup();
}

View File

@ -86,6 +86,7 @@ public:
#endif #endif
Gravity(); Gravity();
~Gravity();
}; };
/*extern int ngrav_enable; //Newtonian gravity /*extern int ngrav_enable; //Newtonian gravity

View File

@ -3197,7 +3197,15 @@ void Simulation::update_particles()//doesn't update the particles themselves, bu
*/ */
} }
Simulation::Simulation() Simulation::~Simulation()
{
free(signs);
delete grav;
delete air;
}
Simulation::Simulation():
sys_pause(0)
{ {
//Create and attach gravity simulation //Create and attach gravity simulation
grav = new Gravity(); grav = new Gravity();

View File

@ -235,6 +235,7 @@ public:
void clear_sim(); void clear_sim();
void UpdateParticles(); void UpdateParticles();
Simulation(); Simulation();
~Simulation();
}; };
//#endif //#endif