Ogre2.1 store weak_ptr in setUserAny

Problems building or running the engine, queries about how to use features etc.
Post Reply
Lax
Hobgoblin
Posts: 583
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany
x 50

Ogre2.1 store weak_ptr in setUserAny

Post by Lax »

Hello all,

is it possible to store in setUserAny a weak_ptr?

It seems odd, but I have the following problem:
I use:

Code: Select all

this->entity->getUserObjectBindings().setUserAny(boost::shared_ptr<GameObject>(gameObjectPtr));
To store a shared_ptr
and later when e.g. selecting a game object, I get it this way:

Code: Select all

gameObjectPtr = Ogre::any_cast<boost::shared_ptr<GameObject>>((targetEntity->getUserObjectBindings().getUserAny()));
But now the mess starts. When I quit my application, I get pointer corruption, because the Ogre Entity maintains my game object, because it maintains the reference count.
So I would like to store a weak_ptr of a game object instead.

I tried to, but I get tons of errors.
I did the following:

Code: Select all

this->entity->getUserObjectBindings().setUserAny(Ogre::Any(boost::weak_ptr<GameObject>(gameObjectPtr)));
and later:

Code: Select all

const Ogre::Any& any = (*itr)->getUserObjectBindings().getUserAny();
boost::weak_ptr<GameObject> weakPtr = Ogre::any_cast<boost::weak_ptr<GameObject>>(any);
GameObjectPtr gameObjectPtr = NOWA::makeStrongPtr(weakPtr);
Or maybe its a bad idea to store a shared_ptr in user any. The problem is, when I select a game object, I need the shared_ptr<GameObject> to feed for other functions...

Regards
Lax

http://www.lukas-kalinowski.com/Homepage/?page_id=1631
Please support Second Earth Technic Base built of Lego bricks for Lego ideas: https://ideas.lego.com/projects/81b9bd1 ... b97b79be62

Post Reply