Skip to content

Commit

Permalink
unload plugin at the right time
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 committed Aug 10, 2024
1 parent acc22c4 commit 83c5624
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions ogre/src/OgreRenderEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef khronos_intptr_t GLintptr;

#include <OgreDynLib.h>
#include <OgreDynLibManager.h>
#include <OgrePlugin.h>

#include <gz/plugin/Register.hh>

Expand Down Expand Up @@ -80,7 +81,6 @@ using namespace rendering;
// unloading it.
// \todo(iche033) Find a proper way to unload the library without causing a
// crash.

typedef void (*DLL_START_PLUGIN)(void);
typedef void (*DLL_STOP_PLUGIN)(void);
std::vector<Ogre::DynLib *> mPluginLibs;
Expand Down Expand Up @@ -124,6 +124,30 @@ void unloadPlugin(Ogre::DynLib *_pluginLib)
delete _pluginLib;
}

/// A dummy plugin class that is installed to the ogre root before shutdown.
/// We use this as a hook to get a callback from ogre root when all plugins
/// are being unloaded. This is so that we can unload the RenderSystem_GL
/// at the right time.
class DummyPlugin : public Ogre::Plugin
{
public: DummyPlugin() {}
public: const Ogre::String& getName() const
{
return pluginName;
}
public: void install() {}
public: void initialise() {}
public: void shutdown() {}
public: void uninstall()
{
for (auto &lib : mPluginLibs)
unloadPlugin(lib);
mPluginLibs.clear();
}
const Ogre::String pluginName = "dummy";
};
DummyPlugin gDummyPlugin;

//////////////////////////////////////////////////
OgreRenderEnginePlugin::OgreRenderEnginePlugin()
{
Expand Down Expand Up @@ -176,24 +200,13 @@ void OgreRenderEngine::Destroy()

if (ogreRoot)
{
// TODO(anyone): do we need to catch segfault on delete?
try
{
// TODO(anyone): do we need to catch segfault on delete?

// Manually shutdown ogre and stop the RenderSystem_GL library
// without unloading it.
// \todo(iche033) replace this whole block with:
// delete this->ogreRoot
// once we are able to unload the RenderSystem_GL library without
// crashing.
this->ogreRoot->shutdown();
this->ogreRoot->destroySceneManager(
this->ogreRoot->_getCurrentSceneManager());
Ogre::TextureManager::getSingletonPtr()->unloadAll();
Ogre::ShadowTextureManager::getSingletonPtr()->clear();
this->ogreRoot->setRenderSystem(nullptr);
for (auto &lib : mPluginLibs)
unloadPlugin(lib);
// Install a dummy plugin that allows use to manually shutdown ogre
// and stop the RenderSystem_GL library.
this->ogreRoot->installPlugin(&gDummyPlugin);

delete this->ogreRoot;
}
catch (...)
Expand Down

0 comments on commit 83c5624

Please sign in to comment.