From e1054d6e073ab8ab4ddc68e09a48a767917defc1 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 21 Jul 2012 20:21:27 +0000 Subject: [PATCH] - fixed memory leaks in map editor, g3dviewer and the game --- source/g3d_viewer/main.cpp | 6 +++++- source/g3d_viewer/renderer.cpp | 12 ++++++++++-- source/glest_map_editor/main.cpp | 4 ++++ source/shared_lib/include/graphics/FileReader.h | 15 +++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/source/g3d_viewer/main.cpp b/source/g3d_viewer/main.cpp index c7522ada..6f27c5cb 100644 --- a/source/g3d_viewer/main.cpp +++ b/source/g3d_viewer/main.cpp @@ -284,7 +284,8 @@ MainWindow::MainWindow( std::pair > unitToLoad, wxSize(Renderer::windowW, Renderer::windowH)), model(NULL), glCanvas(NULL), renderer(NULL), initTextureManager(true), timer(NULL), - startupSettingsInited(false) + startupSettingsInited(false), + fileDialog(NULL) { this->appPath = appPath; Properties::setApplicationPath(executable_path(appPath)); @@ -531,6 +532,9 @@ MainWindow::~MainWindow(){ delete timer; timer = NULL; + delete fileDialog; + fileDialog = NULL; + delete model; model = NULL; diff --git a/source/g3d_viewer/renderer.cpp b/source/g3d_viewer/renderer.cpp index ba2150b2..73816176 100644 --- a/source/g3d_viewer/renderer.cpp +++ b/source/g3d_viewer/renderer.cpp @@ -113,6 +113,11 @@ Renderer::~Renderer() { //resources delete particleManager; delete modelManager; + + if(GraphicsInterface::getInstance().getFactory() != NULL) { + delete GraphicsInterface::getInstance().getFactory(); + GraphicsInterface::getInstance().setFactory(NULL); + } } Renderer * Renderer::getInstance() { @@ -174,8 +179,11 @@ Model * Renderer::getNewModel() { void Renderer::init() { assertGl(); - GraphicsFactory *gf= new GraphicsFactoryGl(); - GraphicsInterface::getInstance().setFactory(gf); + GraphicsFactory *gf= GraphicsInterface::getInstance().getFactory(); + if(gf == NULL) { + gf= new GraphicsFactoryGl(); + GraphicsInterface::getInstance().setFactory(gf); + } modelRenderer= gf->newModelRenderer(); textureManager= gf->newTextureManager(); diff --git a/source/glest_map_editor/main.cpp b/source/glest_map_editor/main.cpp index b96b8671..39b681ba 100644 --- a/source/glest_map_editor/main.cpp +++ b/source/glest_map_editor/main.cpp @@ -80,6 +80,7 @@ MainWindow::MainWindow(string appPath) , menuBar(NULL) , panel(NULL) , glCanvas(NULL) + , fileDialog(NULL) , program(NULL), boxsizer(NULL), startupSettingsInited(false) { menuFile=NULL; @@ -468,6 +469,9 @@ void MainWindow::setupStartupSettings() { } MainWindow::~MainWindow() { + delete fileDialog; + fileDialog = NULL; + delete program; program = NULL; diff --git a/source/shared_lib/include/graphics/FileReader.h b/source/shared_lib/include/graphics/FileReader.h index 6e84ade7..a84eecc7 100644 --- a/source/shared_lib/include/graphics/FileReader.h +++ b/source/shared_lib/include/graphics/FileReader.h @@ -99,6 +99,8 @@ public: * can be read or not depending on the file content*/ virtual bool canRead(ifstream& file) const; + virtual void cleanupExtensions(); + /**Reads a file * This method tries to read the file with the specified filepath * If it fails, either null is returned or an exception @@ -137,6 +139,7 @@ public: virtual T* read(ifstream& file, const string& path, T* former) const = 0; virtual ~FileReader() { + cleanupExtensions(); }; //Well ... these objects aren't supposed to be destroyed }; @@ -283,6 +286,18 @@ FileReader::FileReader(std::vector extensions): extensions(extensions } } +template +void FileReader::cleanupExtensions() { + std::vector nextExtension = extensions; + for(unsigned int i = 0; i < nextExtension.size(); ++i) { + vector const* >* curPossibleReaders = (getFileReadersMap())[nextExtension[i]]; + if (curPossibleReaders != NULL) { + delete curPossibleReaders; + (getFileReadersMap())[nextExtension[i]] = NULL; + } + } +} + /**Gives a quick estimation of whether the specified file * can be read or not depending on the filename*/ template