Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/CoreStaticInitializationHandler.cc @ 10539

Last change on this file since 10539 was 10539, checked in by landauf, 9 years ago

destroy objects before deleting identifiers (when unloading a module)

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "CoreStaticInitializationHandler.h"
30
31#include "CoreIncludes.h"
32#include "module/ModuleInstance.h"
33#include "class/IdentifierManager.h"
34#include "object/Iterator.h"
35
36namespace orxonox
37{
38    void CoreStaticInitializationHandler::setupHandler()
39    {
40        // TODO
41    }
42
43    void CoreStaticInitializationHandler::shutdownHandler()
44    {
45        // TODO
46    }
47
48    void CoreStaticInitializationHandler::loadModule(ModuleInstance* module)
49    {
50        this->loadInstances(module);
51        if (this->bInitInstances_)
52            this->initInstances(module);
53    }
54
55    void CoreStaticInitializationHandler::loadInstances(ModuleInstance* module)
56    {
57        // the order of initialization is important: handlers > identifiers > singletons > everything else
58        module->loadAllStaticallyInitializedInstances(StaticInitialization::STATIC_INITIALIZATION_HANDLER);
59        module->loadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER);
60        module->loadAllStaticallyInitializedInstances(StaticInitialization::SCOPED_SINGLETON_WRAPPER);
61        module->loadAllStaticallyInitializedInstances(StaticInitialization::COMMAND_LINE_ARGUMENT);
62        module->loadAllStaticallyInitializedInstances(StaticInitialization::CONSOLE_COMMAND);
63    }
64
65    void CoreStaticInitializationHandler::initInstances(ModuleInstance* module)
66    {
67        IdentifierManager::getInstance().createClassHierarchy();
68    }
69
70    void CoreStaticInitializationHandler::unloadModule(ModuleInstance* module)
71    {
72        // inverted order of initialization
73        module->unloadAllStaticallyInitializedInstances(StaticInitialization::CONSOLE_COMMAND);
74        module->unloadAllStaticallyInitializedInstances(StaticInitialization::COMMAND_LINE_ARGUMENT);
75        module->unloadAllStaticallyInitializedInstances(StaticInitialization::SCOPED_SINGLETON_WRAPPER);
76
77        // until now every object (including singletons) of the unloaded identifiers should have been destroyed in a controlled manner.
78        // every remaining object is now destroyed in random order.
79        this->destroyObjects(module);
80
81        // all objects are gone now and we can unload identifiers
82        module->unloadAllStaticallyInitializedInstances(StaticInitialization::IDENTIFIER);
83        module->unloadAllStaticallyInitializedInstances(StaticInitialization::STATIC_INITIALIZATION_HANDLER);
84    }
85
86    void CoreStaticInitializationHandler::destroyObjects(ModuleInstance* module)
87    {
88        // collect all identifiers that are about to be unloaded
89        std::set<Identifier*> identifiers;
90        const std::set<StaticallyInitializedInstance*>& instances = module->getInstances(StaticInitialization::IDENTIFIER);
91        for (std::set<StaticallyInitializedInstance*>::const_iterator it = instances.begin(); it != instances.end(); ++it)
92            identifiers.insert(&static_cast<StaticallyInitializedIdentifier*>(*it)->getIdentifier());
93
94        // destroy objects. some objects may survive this at first because they still have smart pointers pointing at them. this is
95        // ok as long as those smart pointers are held by objects that are also about to be destroyed in the same loop. this means
96        // that objects within one module may reference each other by smart pointers. but it is not allowed that objects from another
97        // module (which is not unloaded) uses smart pointers to point at objects inside the unloaded module. this will lead to a crash.
98        for (std::set<Identifier*>::iterator it = identifiers.begin(); it != identifiers.end(); ++it)
99            (*it)->destroyObjects();
100
101        // check if all objects were really destroyed. this is not the case if an object is referenced by a smart pointer from another
102        // module (or if two objects inside this module reference each other). this will lead to a crash and must be fixed (e.g. by
103        // changing object dependencies; or by changing the logic that allows modules to be unloaded).
104        for (std::set<Identifier*>::iterator it = identifiers.begin(); it != identifiers.end(); ++it)
105        {
106            ObjectListBase* objectList = Context::getRootContext()->getObjectList(*it);
107            if (objectList->size() > 0)
108            {
109                orxout(internal_error) << "There are still " << objectList->size() << " objects of type " << (*it)->getName()
110                    << " after unloading the Identifier. This may lead to a crash" << endl;
111            }
112        }
113
114        // destroy object-lists in all contexts
115        for (std::set<Identifier*>::iterator it_identifier = identifiers.begin(); it_identifier != identifiers.end(); ++it_identifier)
116        {
117            // only do this if the Identifier is not a Context itself; otherwise we delete the list we're iterating over
118            if (!(*it_identifier)->isExactlyA(Class(Context)))
119            {
120                // iterate over all contexts
121                for (ObjectList<Context>::iterator it_context = ObjectList<Context>::begin(); it_context != ObjectList<Context>::end(); ++it_context)
122                    it_context->destroyObjectList((*it_identifier));
123            }
124        }
125    }
126}
Note: See TracBrowser for help on using the repository browser.