Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 4, 2008, 8:28:14 PM (16 years ago)
Author:
rgrieder
Message:

Completed destruction of static elements like XMLPort, Identifier, etc.
Of initially about 250 memory leaks (not in the actual meaning but the memory was never freed anyway) only 1 remains in TinyCpp.

  • Core class is now a normal Singleton that gets created and destroyed in main.
  • The same goes for Language, LuaBind, SignalHandler and PlayerManager.
  • Added a new std::set to the CommandExecutor so that the external ConsoleCommands can get destroyed too.
  • Code for destroying CommandLineArguments
  • Added destruction code for ConstructionCallbacks in Identifier
  • Moved internal identifier map (the one with the typeid(.) names) in a static function in Identifier. This was necessary in order to destroy ALL Identifiers with the static destruction function. Before it was possible to create an Identifier with having a class instance (that would call RegisterObject) for instance by simply accessing it via getIdentifier.
  • Removed a big memory leak in Button (forgot to destroy the ConfigValueContainers)
  • Added destruction code for InputBufferListenerTuples in InputBuffer destructor.
  • Added destruction code for load and save executors in both XMLPortParam and XMLPortObject
  • Added destruction code for ConsoleCommands in GSRoot, GSGraphics and GSLevel (temporary solution anyway)
  • Deleting the CEGUILua script module seems to work properly now, one memory leak less (GUIManager.cc)
  • Added global destruction calls in Main.cc
Location:
code/branches/objecthierarchy2/src/core
Files:
21 edited

Legend:

Unmodified
Added
Removed
  • code/branches/objecthierarchy2/src/core/CommandExecutor.cc

    r1784 r2344  
    5353    }
    5454
    55     ConsoleCommand& CommandExecutor::addConsoleCommandShortcut(ConsoleCommand* command)
     55    ConsoleCommand& CommandExecutor::addConsoleCommandShortcut(ConsoleCommand* command, bool bDeleteAtExit)
    5656    {
    5757        std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(command->getName());
     
    6161        }
    6262
     63        // Make sure we can also delete the external ConsoleCommands that don't belong to an Identifier
     64        if (command && bDeleteAtExit)
     65        {
     66            CommandExecutor::getInstance().consoleCommandExternals_.insert(command);
     67        }
    6368
    6469        CommandExecutor::getInstance().consoleCommandShortcuts_[command->getName()] = command;
     
    647652        }
    648653    }
     654
     655    void CommandExecutor::destroyExternalCommands()
     656    {
     657        for (std::set<ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandExternals_.begin();
     658            it != CommandExecutor::getInstance().consoleCommandExternals_.end(); ++it)
     659            delete *it;
     660    }
    649661}
  • code/branches/objecthierarchy2/src/core/CommandExecutor.h

    r1771 r2344  
    5151            static const CommandEvaluation& getLastEvaluation();
    5252
    53             static ConsoleCommand& addConsoleCommandShortcut(ConsoleCommand* command);
     53            static ConsoleCommand& addConsoleCommandShortcut(ConsoleCommand* command, bool bDeleteAtExit = false);
    5454            static ConsoleCommand* getConsoleCommandShortcut(const std::string& name);
    5555            static ConsoleCommand* getLowercaseConsoleCommandShortcut(const std::string& name);
     
    6868            /** @brief Returns a const_iterator to the end of the map that stores all console commands with their names in lowercase. @return The const_iterator */
    6969            static inline std::map<std::string, ConsoleCommand*>::const_iterator getLowercaseConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end(); }
     70
     71            static void destroyExternalCommands();
    7072
    7173        private:
     
    101103            std::map<std::string, ConsoleCommand*> consoleCommandShortcuts_;
    102104            std::map<std::string, ConsoleCommand*> consoleCommandShortcuts_LC_;
     105            std::set<ConsoleCommand*>              consoleCommandExternals_;
    103106    }; // tolua_export
    104107} // tolua_export
  • code/branches/objecthierarchy2/src/core/CommandLine.cc

    r2105 r2344  
    8383    CommandLine::~CommandLine()
    8484    {
    85         for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
    86             it != cmdLineArgs_.end(); ++it)
    87         {
    88             delete it->second;
    89         }
     85        CommandLine::destroyAllArguments();
    9086    }
    9187
     
    9894        static CommandLine instance;
    9995        return instance;
     96    }
     97
     98    /**
     99    @brief
     100        Destroys all command line arguments. This should be called at the end
     101        of main. Do not use before that.
     102    */
     103    void CommandLine::destroyAllArguments()
     104    {
     105        for (std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.begin();
     106            it != _getInstance().cmdLineArgs_.end(); ++it)
     107            delete it->second;
     108        _getInstance().cmdLineArgs_.clear();
    100109    }
    101110
  • code/branches/objecthierarchy2/src/core/CommandLine.h

    r2103 r2344  
    155155        }
    156156
     157        static void destroyAllArguments();
    157158
    158159    private:
     
    179180        //! Holds all pointers to the arguments and serves as a search map by name.
    180181        std::map<std::string, CommandLineArgument*> cmdLineArgs_;
    181         //! Search map by chortcut for the arguments.
     182        //! Search map by shortcut for the arguments.
    182183        std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_;
    183184    };
  • code/branches/objecthierarchy2/src/core/ConsoleCommand.h

    r2087 r2344  
    6464
    6565#define SetConsoleCommandShortcutGeneric(fakevariable, command) \
    66     orxonox::ConsoleCommand& fakevariable = orxonox::CommandExecutor::addConsoleCommandShortcut(command)
     66    orxonox::ConsoleCommand& fakevariable = orxonox::CommandExecutor::addConsoleCommandShortcut(command, true)
    6767
    6868
  • code/branches/objecthierarchy2/src/core/Core.cc

    r2171 r2344  
    4646    bool Core::bIsMaster_s      = false;
    4747
     48    Core* Core::singletonRef_s = 0;
     49
    4850    /**
    4951        @brief Constructor: Registers the object and sets the config-values.
     
    5355    {
    5456        RegisterRootObject(Core);
     57
     58        assert(singletonRef_s == 0);
     59        singletonRef_s = this;
     60
    5561        this->setConfigValues();
    56         isCreatingCoreSettings() = false;
    5762    }
    5863
     
    6267    Core::~Core()
    6368    {
    64         isCreatingCoreSettings() = true;
    65     }
    66 
    67     /**
    68         @brief Returns true if the Core instance is not yet ready and the static functions have to return a default value.
    69     */
    70     bool& Core::isCreatingCoreSettings()
    71     {
    72         static bool bCreatingCoreSettings = true;
    73         return bCreatingCoreSettings;
    74     }
    75 
    76     /**
    77         @brief Returns a unique instance of Core.
    78         @return The instance
    79     */
    80     Core& Core::getInstance()
    81     {
    82         // If bCreatingSoftDebugLevelObject is true, we're just about to create an instance of the DebugLevel class
    83         //if (Core::isCreatingCoreSettings())
    84         //{
    85         //    isCreatingCoreSettings() = false;
    86         //    //instance.setConfigValues();
    87         //}
    88 
    89         static bool firstTime = true;
    90         if (firstTime)
    91             isCreatingCoreSettings() = true;
    92 
    93         static Core instance;
    94         return instance;
     69        assert(singletonRef_s);
     70        singletonRef_s = 0;
    9571    }
    9672
     
    140116    int Core::getSoftDebugLevel(OutputHandler::OutputDevice device)
    141117    {
    142         if (!Core::isCreatingCoreSettings())
     118        switch (device)
    143119        {
    144             switch (device)
    145             {
    146             case OutputHandler::LD_All:
    147                 return Core::getInstance().softDebugLevel_;
    148             case OutputHandler::LD_Console:
    149                 return Core::getInstance().softDebugLevelConsole_;
    150             case OutputHandler::LD_Logfile:
    151                 return Core::getInstance().softDebugLevelLogfile_;
    152             case OutputHandler::LD_Shell:
    153                 return Core::getInstance().softDebugLevelShell_;
    154             default:
    155                 assert(0);
    156             }
     120        case OutputHandler::LD_All:
     121            return Core::getInstance().softDebugLevel_;
     122        case OutputHandler::LD_Console:
     123            return Core::getInstance().softDebugLevelConsole_;
     124        case OutputHandler::LD_Logfile:
     125            return Core::getInstance().softDebugLevelLogfile_;
     126        case OutputHandler::LD_Shell:
     127            return Core::getInstance().softDebugLevelShell_;
     128        default:
     129            assert(0);
     130            return 2;
    157131        }
    158 
    159         // Return a constant value while we're creating the object
    160         return 2;
    161132    }
    162133
     
    168139     void Core::setSoftDebugLevel(OutputHandler::OutputDevice device, int level)
    169140     {
    170         if (!Core::isCreatingCoreSettings())
    171         {
    172             if (device == OutputHandler::LD_All)
    173                 Core::getInstance().softDebugLevel_ = level;
    174             else if (device == OutputHandler::LD_Console)
    175                 Core::getInstance().softDebugLevelConsole_ = level;
    176             else if (device == OutputHandler::LD_Logfile)
    177                 Core::getInstance().softDebugLevelLogfile_ = level;
    178             else if (device == OutputHandler::LD_Shell)
    179                 Core::getInstance().softDebugLevelShell_ = level;
     141        if (device == OutputHandler::LD_All)
     142            Core::getInstance().softDebugLevel_ = level;
     143        else if (device == OutputHandler::LD_Console)
     144            Core::getInstance().softDebugLevelConsole_ = level;
     145        else if (device == OutputHandler::LD_Logfile)
     146            Core::getInstance().softDebugLevelLogfile_ = level;
     147        else if (device == OutputHandler::LD_Shell)
     148            Core::getInstance().softDebugLevelShell_ = level;
    180149
    181             OutputHandler::setSoftDebugLevel(device, level);
    182         }
     150        OutputHandler::setSoftDebugLevel(device, level);
    183151     }
    184152
     
    188156    const std::string& Core::getLanguage()
    189157    {
    190         if (!Core::isCreatingCoreSettings())
    191             return Core::getInstance().language_;
    192 
    193         return Language::getLanguage().defaultLanguage_;
     158        return Core::getInstance().language_;
    194159    }
    195160
  • code/branches/objecthierarchy2/src/core/Core.h

    r2171 r2344  
    4040#include "CorePrereqs.h"
    4141
     42#include <cassert>
    4243#include "OrxonoxClass.h"
    4344#include "util/OutputHandler.h"
     
    4950    {
    5051        public:
    51             static Core& getInstance();
    52             static bool& isCreatingCoreSettings();
     52            Core();
     53            ~Core();
    5354            void setConfigValues();
    5455            void debugLevelChanged();
    5556            void languageChanged();
    5657
    57             static int getSoftDebugLevel(OutputHandler::OutputDevice device = OutputHandler::LD_All);
    58             static void setSoftDebugLevel(OutputHandler::OutputDevice device, int level);
     58            static Core& getInstance() { assert(singletonRef_s); return *singletonRef_s; }
     59
     60            static int   getSoftDebugLevel(OutputHandler::OutputDevice device = OutputHandler::LD_All);
     61            static void  setSoftDebugLevel(OutputHandler::OutputDevice device, int level);
    5962            static const std::string& getLanguage();
    60             static void resetLanguage();
     63            static void  resetLanguage();
    6164
    6265            // fast access global variables.
     
    7376
    7477        private:
     78            Core(const Core&);
    7579            void resetLanguageIntern();
    76 
    77             Core();
    78             Core(const Core& other);
    79             virtual ~Core();
    8080
    8181            int softDebugLevel_;                            //!< The debug level
     
    9090            static bool bIsStandalone_s;
    9191            static bool bIsMaster_s;
     92
     93            static Core* singletonRef_s;
    9294    };
    9395}
  • code/branches/objecthierarchy2/src/core/CorePrereqs.h

    r2173 r2344  
    133133  class LanguageEntry;
    134134  class Loader;
     135  class LuaBind;
    135136  class MetaObjectList;
    136137  class MetaObjectListElement;
  • code/branches/objecthierarchy2/src/core/Identifier.cc

    r2171 r2344  
    9393        for (std::map<std::string, XMLPortObjectContainer*>::iterator it = this->xmlportObjectContainers_.begin(); it != this->xmlportObjectContainers_.end(); ++it)
    9494            delete (it->second);
     95        for (std::vector<Functor*>::iterator it = this->constructionCallbacks_.begin(); it != this->constructionCallbacks_.end(); ++it)
     96            delete *it;
     97    }
     98
     99    /**
     100        @brief Returns the identifier map with the names as received by typeid(). This is only used internally.
     101    */
     102    std::map<std::string, Identifier*>& Identifier::getTypeIDIdentifierMap()
     103    {
     104        static std::map<std::string, Identifier*> identifiers;    //!< The map to store all Identifiers.
     105        return identifiers;
    95106    }
    96107
     
    103114    Identifier* Identifier::getIdentifierSingleton(const std::string& name, Identifier* proposal)
    104115    {
    105         static std::map<std::string, Identifier*> identifiers;    //!< The map to store all Identifiers.
    106         std::map<std::string, Identifier*>::const_iterator it = identifiers.find(name);
    107 
    108         if (it != identifiers.end())
     116        std::map<std::string, Identifier*>::const_iterator it = getTypeIDIdentifierMap().find(name);
     117
     118        if (it != getTypeIDIdentifierMap().end())
    109119        {
    110120            // There is already an entry: return it and delete the proposal
     
    115125        {
    116126            // There is no entry: put the proposal into the map and return it
    117             identifiers[name] = proposal;
     127            getTypeIDIdentifierMap()[name] = proposal;
    118128            return proposal;
    119129        }
     
    192202    void Identifier::destroyAllIdentifiers()
    193203    {
    194         for (std::map<std::string, Identifier*>::iterator it = Identifier::getIdentifierMapIntern().begin(); it != Identifier::getIdentifierMapIntern().end(); ++it)
     204        for (std::map<std::string, Identifier*>::iterator it = Identifier::getTypeIDIdentifierMap().begin(); it != Identifier::getTypeIDIdentifierMap().end(); ++it)
    195205            delete (it->second);
    196206    }
  • code/branches/objecthierarchy2/src/core/Identifier.h

    r2171 r2344  
    257257            void initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass);
    258258
     259            static void destroyAllIdentifiers();
     260
    259261        protected:
    260262            Identifier();
     
    299301            }
    300302
     303            static std::map<std::string, Identifier*>& getTypeIDIdentifierMap();
     304
    301305            void initialize(std::set<const Identifier*>* parents);
    302 
    303             static void destroyAllIdentifiers();
    304306
    305307            std::set<const Identifier*> parents_;                          //!< The parents of the class the Identifier belongs to
  • code/branches/objecthierarchy2/src/core/Language.cc

    r2171 r2344  
    8787    // ###        Language         ###
    8888    // ###############################
     89
     90    Language* Language::singletonRef_s = 0;
     91
    8992    /**
    9093        @brief Constructor: Reads the default language file and sets some values.
     
    9295    Language::Language()
    9396    {
     97        assert(singletonRef_s == 0);
     98        singletonRef_s = this;
     99
    94100        this->defaultLanguage_ = "default";
    95101        this->defaultLocalisation_ = "ERROR: LANGUAGE ENTRY DOESN'T EXIST!";
     
    106112        for (std::map<std::string, LanguageEntry*>::iterator it = this->languageEntries_.begin(); it != this->languageEntries_.end(); ++it)
    107113            delete (it->second);
    108     }
    109 
    110     /**
    111         @brief Returns a reference to the only existing instance of the Language class and calls the setConfigValues() function.
    112         @return The reference to the only existing instance
    113     */
    114     Language& Language::getLanguage()
    115     {
    116         static Language instance = Language();
    117         return instance;
     114
     115        assert(singletonRef_s);
     116        singletonRef_s = 0;
    118117    }
    119118
  • code/branches/objecthierarchy2/src/core/Language.h

    r2171 r2344  
    5050#include <map>
    5151#include <string>
     52#include <cassert>
    5253
    5354#define AddLanguageEntry(label, fallbackstring) \
     
    116117
    117118        public:
    118             static Language& getLanguage();
     119            Language();
     120            ~Language();
     121
     122            static Language& getLanguage() { assert(singletonRef_s); return *singletonRef_s; }
    119123            void addEntry(const LanguageEntryLabel& label, const std::string& entry);
    120124            const std::string& getLocalisation(const LanguageEntryLabel& label) const;
    121125
    122126        private:
    123             Language();
    124             Language(const Language& language);     // don't copy
    125             virtual ~Language();
     127            Language(const Language&);
    126128
    127129            void readDefaultLanguageFile();
     
    134136            std::string defaultLocalisation_;                       //!< The returned string, if an entry unavailable entry is requested
    135137            std::map<std::string, LanguageEntry*> languageEntries_; //!< A map to store all LanguageEntry objects and their labels
     138
     139            static Language* singletonRef_s;
    136140    };
    137141}
  • code/branches/objecthierarchy2/src/core/Loader.cc

    r2171 r2344  
    120120
    121121        // let Lua work this out:
    122         LuaBind* lua = LuaBind::getInstance();
    123         lua->clearLuaOutput();
    124         lua->loadFile(file->getFilename(), true);
    125         lua->run();
     122        LuaBind& lua = LuaBind::getInstance();
     123        lua.clearLuaOutput();
     124        lua.loadFile(file->getFilename(), true);
     125        lua.run();
    126126
    127127        try
     
    135135            ticpp::Document xmlfile;
    136136            //xmlfile.ToDocument();
    137             xmlfile.Parse(lua->getLuaOutput(), true);
     137            xmlfile.Parse(lua.getLuaOutput(), true);
    138138
    139139            ticpp::Element rootElement;
  • code/branches/objecthierarchy2/src/core/LuaBind.cc

    r2087 r2344  
    4040namespace orxonox
    4141{
    42   LuaBind* LuaBind::singletonRef = NULL;
     42  LuaBind* LuaBind::singletonRef_s = NULL;
    4343
    4444  LuaBind::LuaBind()
    4545  {
     46    assert(LuaBind::singletonRef_s == 0);
     47    LuaBind::singletonRef_s = this;
     48
    4649    luaState_ = lua_open();
    4750    luaSource_ = "";
  • code/branches/objecthierarchy2/src/core/LuaBind.h

    r2087 r2344  
    4242}
    4343
     44#include <cassert>
    4445#include <list>
    4546#include <string>
     
    5859
    5960    public:
    60       inline static LuaBind* getInstance() { if (!LuaBind::singletonRef) LuaBind::singletonRef = new LuaBind(); return LuaBind::singletonRef; } // tolua_export
    61       inline ~LuaBind() { LuaBind::singletonRef = NULL; };
     61      LuaBind();
     62      inline ~LuaBind() { assert(singletonRef_s); LuaBind::singletonRef_s = NULL; };
     63
     64      inline static LuaBind& getInstance() { assert(singletonRef_s); return *LuaBind::singletonRef_s; } // tolua_export
    6265
    6366    void loadFile(std::string filename, bool luaTags);
     
    8386
    8487    private:
    85       LuaBind();
    86       static LuaBind* singletonRef;
     88      static LuaBind* singletonRef_s;
    8789
    8890      std::string luaSource_;
  • code/branches/objecthierarchy2/src/core/RootGameState.cc

    r2103 r2344  
    3131#include "util/Debug.h"
    3232#include "util/Exception.h"
    33 #include "Core.h"
    3433#include "Clock.h"
    3534#include "CommandLine.h"
     
    137136            Clock clock;
    138137
    139             // create the Core settings to configure the output level
    140             Core::getInstance();
    141 
    142138            this->activate();
    143139
  • code/branches/objecthierarchy2/src/core/RootGameState.h

    r2103 r2344  
    4848        void gotoState(const std::string& name);
    4949
    50         std::string           stateRequest_;
     50        std::string stateRequest_;
    5151    };
    5252}
  • code/branches/objecthierarchy2/src/core/XMLPort.h

    r2173 r2344  
    4343#include "CorePrereqs.h"
    4444
     45#include <cassert>
    4546#include "util/Debug.h"
    4647#include "util/Exception.h"
     
    327328                this->loadexecutor_ = loadexecutor;
    328329                this->saveexecutor_ = saveexecutor;
     330            }
     331
     332            ~XMLPortClassParamContainer()
     333            {
     334                assert(this->loadexecutor_);
     335                delete this->loadexecutor_;
     336                if (this->saveexecutor_)
     337                    delete this->saveexecutor_;
    329338            }
    330339
     
    471480                this->bApplyLoaderMask_ = bApplyLoaderMask;
    472481                this->bLoadBefore_ = bLoadBefore;
     482            }
     483
     484            ~XMLPortClassObjectContainer()
     485            {
     486                assert(this->loadexecutor_);
     487                delete this->loadexecutor_;
     488                if (this->saveexecutor_)
     489                    delete this->saveexecutor_;
    473490            }
    474491
  • code/branches/objecthierarchy2/src/core/input/Button.cc

    r2103 r2344  
    5959        nCommands_[1]=0;
    6060        nCommands_[2]=0;
     61        this->configContainer_ = 0;
    6162        clear();
    6263    }
     
    8081            }
    8182        }
     83
     84        if (this->configContainer_)
     85            delete this->configContainer_;
     86        this->configContainer_ = 0;
    8287    }
    8388
  • code/branches/objecthierarchy2/src/core/input/InputBuffer.cc

    r1755 r2344  
    7373    }
    7474
     75    InputBuffer::~InputBuffer()
     76    {
     77        for (std::list<BaseInputBufferListenerTuple*>::const_iterator it = this->listeners_.begin();
     78            it != this->listeners_.end(); ++it)
     79            delete *it;
     80    }
     81
    7582    void InputBuffer::setConfigValues()
    7683    {
  • code/branches/objecthierarchy2/src/core/input/InputBuffer.h

    r1887 r2344  
    7979        public:
    8080            InputBuffer();
     81            ~InputBuffer();
    8182            InputBuffer(const std::string allowedChars);
    8283
Note: See TracChangeset for help on using the changeset viewer.