In my game I got a BaseObject class, and a ObjectManager class.
typedef boost::shared_ptr< BaseObject > BaseObjectPtr;
typedef boost::weak_ptr< BaseObject > BaseObjectWPtr;
typedef boost::shared_ptr< Entity > EntityPtr;
typedef boost::weak_ptr< Entity > EntityWPtr;
The objectmanager, stores a BaseObjectWPtr to each of the object it 'manages'.
This weak pointer is .lock()ed when it should be retrived.
This part of the code works fine. The problem comes when I take a
BaseObject derived class Entity and tries to store it in the manager.
I can store it alright, and retrive it. But when I try to cast it back
into a EntityPtr, it throws some weird
exception that I cant seem to find. (tried: boost::bad_weak_ptr, std::bad_cast).
The code that im using to test it:
// create the object
EntityPtr e1( new Entity );
e1->SetObjectName( "e1" );
ObjectManager::Instance()->Register( e1 );
// this will make the Manager store a BaseObjectWPtr to the
// object, and that WPtr will be .lock()ed when I retrive it.
// retrive it
BaseObjectPtr obj = ObjectManager::Instance()->GetObjectWithName( "e1" );
// using my own custom RTTI I can check the type and the name
// from log:
// obj type:Entity
// obj name:e1
std::cout << "obj type:" << obj->GetType().GetName() << std::endl;
std::cout << "obj name:" << obj->GetObjectName() << std::endl;
// here I try to cast it into a EntityPtr.
// I am sure the object is VALID as I have tried a
// BaseObject method on it. (see above)
// but this cast fail as some weird exception gets thrown.
// WHY ??
EntityPtr e2( boost::dynamic_pointer_cast<Entity>(obj) );
///////////////////////////////
// this way, do work thought
(( Entity* )obj.get())->Do(); // Do is a Entity only method.
As you can see I have no idea why this wont work.
Im using boost_1_33_0. If you have any idea please help me out here!