Jump to content
  • Advertisement
Sign in to follow this  

boost pointer cast, keeps throwing strange exception

This topic is 4609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post

Link to post
Share on other sites
I have no idea what's wrong with your code. But you could identify the exception with RTTI (if it subclasses std::exception).

try {
EntityPtr e2( boost::dynamic_pointer_cast<Entity>(obj) );
} catch (std::exception& e) {
cout << typeid(e).name() << endl;

Share this post

Link to post
Share on other sites
boost::dynamic_pointer_cast is documented as not throwing any exceptions, which suggests you've overwritten your vtable pointer or the information the compiler uses to to perform checked downcasts or the information boost::shared_ptr uses to maintain reference counts.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!