boost pointer cast, keeps throwing strange exception

Started by
2 comments, last by Shannon Barber 18 years ago
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!
- Me
Advertisement
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;}
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.

Σnigma
If catching std::exception doesn't get it, try catching int, this will grab any Win32 SEH exceptions.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement