Using shared_ptrs with Ogre

Problems building or running the engine, queries about how to use features etc.
Post Reply
Owl53
Halfling
Posts: 92
Joined: Sat Jul 22, 2017 2:32 pm
x 4

Using shared_ptrs with Ogre

Post by Owl53 »

Ogre Version: 1.11
Operating System: Windows 10
Render System: OpenGL

Hi,

Could someone clarify for me please how to handle the clean-up of specifically Ogre objects that have been created using shared_ptrs? In this particular case, various objects have been created using Ogre Overlay, as shown below:

Code: Select all

mOverlaySystem.reset(new Ogre::OverlaySystem());
mSceneManager->addRenderQueueListener(mOverlaySystem.get());
mOverlay.reset(Ogre::OverlayManager::getSingleton().create("Overlay"));

mPanel.reset(static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Test")));
mPanel->setMetricsMode(Ogre::GMM_PIXELS);
mPanel->setPosition(0.0f, 0.0f);
mPanel->setDimensions(1.0f, 1.0f);

Ogre::FontPtr pFont = Ogre::FontManager::getSingletonPtr()->create("georgia", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
pFont->setType(Ogre::FT_TRUETYPE);
pFont->setSource("georgia.ttf");
pFont->setTrueTypeSize(16);
pFont->load();

mOverlay->add2D(m_panel.get());
mOverlay->show();
After closing the program, the shared_ptrs handle the clean up of the objects as would be expected. As a result, an access violation occurs in OgreOverlayManager.cpp in the following function:

Code: Select all

 void OverlayManager::destroyAll(void)
{
	for (OverlayMap::iterator i = mOverlayMap.begin();
            i != mOverlayMap.end(); ++i)
        {
            OGRE_DELETE i->second; //Access violation here
        }
        mOverlayMap.clear();
        mLoadedScripts.clear();
}
Presumably this is because the Overlay it is looking to delete has already been deleted by the shared_ptr. So is there a correct way to be able to use shared_ptrs with Ogre to avoid this double deletion?

Thanks in advance
paroj
OGRE Team Member
OGRE Team Member
Posts: 1994
Joined: Sun Mar 30, 2014 2:51 pm
x 1074
Contact:

Re: Using shared_ptrs with Ogre

Post by paroj »

all raw pointers returned by Ogre are owned by Ogre. Therefore this is wrong:

Code: Select all

mPanel.reset(static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Test")));
Owl53
Halfling
Posts: 92
Joined: Sat Jul 22, 2017 2:32 pm
x 4

Re: Using shared_ptrs with Ogre

Post by Owl53 »

paroj wrote: Tue Nov 20, 2018 5:01 pm all raw pointers returned by Ogre are owned by Ogre. Therefore this is wrong:

Code: Select all

mPanel.reset(static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Test")));
Sorry, I'm missing something here. What should it be instead? :oops:
paroj
OGRE Team Member
OGRE Team Member
Posts: 1994
Joined: Sun Mar 30, 2014 2:51 pm
x 1074
Contact:

Re: Using shared_ptrs with Ogre

Post by paroj »

Code: Select all

Ogre::OverlayContainer* mPanel =static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Test"));
Owl53
Halfling
Posts: 92
Joined: Sat Jul 22, 2017 2:32 pm
x 4

Re: Using shared_ptrs with Ogre

Post by Owl53 »

paroj wrote: Tue Nov 20, 2018 5:35 pm

Code: Select all

Ogre::OverlayContainer* mPanel =static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Test"));
I see, so its not a viable option to use smart pointers in these situations?

Edit: Ahh, right, now I see what you mean by the owned by Ogre. I will have to avoid shared_ptrs for the Ogre pointers then. Thanks
Post Reply