Index: code/trunk/src/orxonox/GraphicsManager.cc
===================================================================
--- code/trunk/src/orxonox/GraphicsManager.cc	(revision 3196)
+++ code/trunk/src/orxonox/GraphicsManager.cc	(revision 3280)
@@ -38,4 +38,5 @@
 #include <fstream>
 #include <boost/filesystem.hpp>
+#include <boost/shared_ptr.hpp>
 
 #include <OgreCompositorManager.h>
@@ -53,5 +54,5 @@
 #include "SpecialConfig.h"
 #include "util/Exception.h"
-#include "util/String.h"
+#include "util/StringUtils.h"
 #include "util/SubString.h"
 #include "core/Clock.h"
@@ -70,4 +71,6 @@
 namespace orxonox
 {
+    using boost::shared_ptr;
+
     class _OrxonoxExport OgreWindowEventListener : public Ogre::WindowEventListener
     {
@@ -89,5 +92,5 @@
         , renderWindow_(0)
         , viewport_(0)
-        , ogreWindowEventListener_(0)
+        , ogreWindowEventListener_(new OgreWindowEventListener())
     {
         RegisterObject(GraphicsManager);
@@ -96,30 +99,35 @@
         singletonRef_s = this;
 
-        this->loaded_ = false;
-
         this->setConfigValues();
-    }
-
-    void GraphicsManager::initialise()
-    {
+
         // Ogre setup procedure
         setupOgre();
-        // load all the required plugins for Ogre
-        loadOgrePlugins();
-        // read resource declaration file
-        this->declareResources();
-        // Reads ogre config and creates the render window
-        this->loadRenderer();
-
-        // TODO: Spread this
-        this->initialiseResources();
-
-        // add console commands
-        FunctorMember<GraphicsManager>* functor1 = createFunctor(&GraphicsManager::printScreen);
-        functor1->setObject(this);
-        ccPrintScreen_ = createConsoleCommand(functor1, "printScreen");
-        CommandExecutor::addConsoleCommandShortcut(ccPrintScreen_);
-
-        this->loaded_ = true;
+
+        try
+        {
+            // load all the required plugins for Ogre
+            loadOgrePlugins();
+            // read resource declaration file
+            this->declareResources();
+            // Reads ogre config and creates the render window
+            this->loadRenderer();
+
+            // TODO: Spread this
+            this->initialiseResources();
+
+            // add console commands
+            FunctorMember<GraphicsManager>* functor1 = createFunctor(&GraphicsManager::printScreen);
+            functor1->setObject(this);
+            ccPrintScreen_ = createConsoleCommand(functor1, "printScreen");
+            CommandExecutor::addConsoleCommandShortcut(ccPrintScreen_);
+        }
+        catch (...)
+        {
+            // clean up
+            delete this->ogreRoot_;
+            delete this->ogreLogger_;
+            delete this->ogreWindowEventListener_;
+            throw;
+        }
     }
 
@@ -130,33 +138,20 @@
     GraphicsManager::~GraphicsManager()
     {
-        if (this->loaded_)
-        {
-            delete this->ccPrintScreen_;
-
-            if (this->ogreWindowEventListener_)
-            {
-                // remove our WindowEventListener first to avoid bad calls after the window has been destroyed
-                Ogre::WindowEventUtilities::removeWindowEventListener(this->renderWindow_, this->ogreWindowEventListener_);
-                delete this->ogreWindowEventListener_;
-            }
-
-            // destroy render window
-//            Ogre::RenderSystem* renderer = this->ogreRoot_->getRenderSystem();
-//            renderer->destroyRenderWindow("Orxonox");
-
-            // HACK! This fixes an exit crash
-            Map::hackDestroyMap();
-
-            // unload all compositors
-            Ogre::CompositorManager::getSingleton().removeAll();
-
-            // Delete OGRE main control organ
-            delete this->ogreRoot_;
-
-            // delete the ogre log and the logManager (since we have created it in the first place).
-            this->ogreLogger_->getDefaultLog()->removeListener(this);
-            this->ogreLogger_->destroyLog(Ogre::LogManager::getSingleton().getDefaultLog());
-            delete this->ogreLogger_;
-        }
+/*
+        delete this->ccPrintScreen_;
+*/
+
+        // HACK! This fixes an exit crash
+        Map::hackDestroyMap();
+        // unload all compositors (this is only necessary because we don't yet destroy all resources!)
+        Ogre::CompositorManager::getSingleton().removeAll();
+
+        // Delete OGRE main control organ
+        delete this->ogreRoot_;
+
+        // delete the logManager (since we have created it in the first place).
+        delete this->ogreLogger_;
+
+        delete this->ogreWindowEventListener_;
 
         assert(singletonRef_s);
@@ -194,27 +189,24 @@
     void GraphicsManager::update(const Clock& time)
     {
-        if (this->loaded_)
-        {
-            Ogre::FrameEvent evt;
-            evt.timeSinceLastFrame = time.getDeltaTime();
-            evt.timeSinceLastEvent = time.getDeltaTime(); // note: same time, but shouldn't matter anyway
-
-            // don't forget to call _fireFrameStarted to OGRE to make sure
-            // everything goes smoothly
-            ogreRoot_->_fireFrameStarted(evt);
-
-            // Pump messages in all registered RenderWindows
-            // This calls the WindowEventListener objects.
-            Ogre::WindowEventUtilities::messagePump();
-            // make sure the window stays active even when not focused
-            // (probably only necessary on windows)
-            this->renderWindow_->setActive(true);
-
-            // render
-            ogreRoot_->_updateAllRenderTargets();
-
-            // again, just to be sure OGRE works fine
-            ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
-        }
+        Ogre::FrameEvent evt;
+        evt.timeSinceLastFrame = time.getDeltaTime();
+        evt.timeSinceLastEvent = time.getDeltaTime(); // note: same time, but shouldn't matter anyway
+
+        // don't forget to call _fireFrameStarted to OGRE to make sure
+        // everything goes smoothly
+        ogreRoot_->_fireFrameStarted(evt);
+
+        // Pump messages in all registered RenderWindows
+        // This calls the WindowEventListener objects.
+        Ogre::WindowEventUtilities::messagePump();
+        // make sure the window stays active even when not focused
+        // (probably only necessary on windows)
+        this->renderWindow_->setActive(true);
+
+        // render
+        ogreRoot_->_updateAllRenderTargets();
+
+        // again, just to be sure OGRE works fine
+        ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
     }
 
@@ -248,10 +240,10 @@
         // create a new logManager
         // Ogre::Root will detect that we've already created a Log
-        ogreLogger_ = new Ogre::LogManager();
+        std::auto_ptr<Ogre::LogManager> logger(new Ogre::LogManager());
         COUT(4) << "Ogre LogManager created" << std::endl;
 
         // create our own log that we can listen to
         Ogre::Log *myLog;
-        myLog = ogreLogger_->createLog(ogreLogFilepath.string(), true, false, false);
+        myLog = logger->createLog(ogreLogFilepath.string(), true, false, false);
         COUT(4) << "Ogre Log created" << std::endl;
 
@@ -272,4 +264,6 @@
         // Leave plugins file empty. We're going to do that part manually later
         ogreRoot_ = new Ogre::Root("", ogreConfigFilepath.string(), ogreLogFilepath.string());
+        // In case that new Root failed the logger gets destroyed because of the std::auto_ptr
+        ogreLogger_ = logger.release();
 
         COUT(3) << "Ogre set up done." << std::endl;
@@ -348,5 +342,5 @@
         if (!ogreRoot_->restoreConfig())
             if (!ogreRoot_->showConfigDialog())
-                ThrowException(InitialisationFailed, "Could not show Ogre configuration dialogue.");
+                ThrowException(InitialisationFailed, "OGRE graphics configuration dialogue failed.");
 
         CCOUT(4) << "Creating render window" << std::endl;
@@ -354,5 +348,4 @@
         this->renderWindow_ = ogreRoot_->initialise(true, "Orxonox");
 
-        this->ogreWindowEventListener_ = new OgreWindowEventListener();
         Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, ogreWindowEventListener_);
 
