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)));
}
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);
#endif
Graphics();
~Graphics();
};
#endif

View File

@ -124,4 +124,7 @@ int main(int argc, char * argv[])
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);
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 prepare_graphicscache();
Renderer(Graphics * g, Simulation * sim);
~Renderer();
};
#endif

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -48,6 +48,9 @@ void Gravity::gravity_init()
gravx = (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));
#ifdef GRAVFFT
grav_fft_init();
#endif
}
void Gravity::gravity_cleanup()
@ -55,6 +58,17 @@ void Gravity::gravity_cleanup()
#ifdef GRAVFFT
grav_fft_cleanup();
#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()
@ -124,9 +138,6 @@ void Gravity::update_grav_async()
//memset(th_gravy, 0, XRES*YRES*sizeof(float));
//memset(th_gravx, 0, XRES*YRES*sizeof(float));
//memset(th_gravp, 0, XRES*YRES*sizeof(float));
#ifdef GRAVFFT
grav_fft_init();
#endif
while(!thread_done){
if(!done){
update_grav();
@ -486,3 +497,8 @@ Gravity::Gravity()
{
gravity_init();
}
Gravity::~Gravity()
{
gravity_cleanup();
}

View File

@ -86,6 +86,7 @@ public:
#endif
Gravity();
~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
grav = new Gravity();

View File

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