Using shared_ptrs with Ogre

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

Using shared_ptrs with Ogre

Post by Owl53 » Tue Nov 20, 2018 3:26 pm

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
0 x

paroj
OGRE Team Member
OGRE Team Member
Posts: 817
Joined: Sun Mar 30, 2014 2:51 pm
x 134
Contact:

Re: Using shared_ptrs with Ogre

Post by paroj » 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")));
0 x

Owl53
Halfling
Posts: 83
Joined: Sat Jul 22, 2017 2:32 pm
x 4

Re: Using shared_ptrs with Ogre

Post by Owl53 » Tue Nov 20, 2018 5:24 pm

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:
0 x

paroj
OGRE Team Member
OGRE Team Member
Posts: 817
Joined: Sun Mar 30, 2014 2:51 pm
x 134
Contact:

Re: Using shared_ptrs with Ogre

Post by paroj » Tue Nov 20, 2018 5:35 pm

Code: Select all

Ogre::OverlayContainer* mPanel =static_cast<Ogre::OverlayContainer*>(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "Test"));
0 x

Owl53
Halfling
Posts: 83
Joined: Sat Jul 22, 2017 2:32 pm
x 4

Re: Using shared_ptrs with Ogre

Post by Owl53 » Tue Nov 20, 2018 5:39 pm

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
0 x

Post Reply