11 Nov, 2010, balakoth wrote in the 1st comment:
Votes: 0
// Handles all brain input
void IWorld::HandleInput( )
{
std::list< IBrain * >::iterator brain;

// This is for handling input.
// Loops through brains, checks to see if there's input to play with,
// then goes on that.
if ( _brainList.empty() != true )
{
for ( brain = _brainList.begin(); brain != _brainList.end(); ++brain )
{
if((*brain)->GetType() == BRAIN_HUMAN )
{
if((*brain)->GetThing() != NULL)
{
if ((*brain)->GetThing()->wait > 0)
{
–(*brain)->GetThing()->wait;
continue;
}
}
}
else
continue;

// Dynamic Casts to handle a human brain
if ( (dynamic_cast<IBrainHuman*>(*brain))->GotInput() == true )
{
(dynamic_cast<IBrainHuman*>(*brain))->HandleInput( (dynamic_cast<IBrainHuman*>(*brain))->GetSocket()->GetInput() );
(dynamic_cast<IBrainHuman*>(*brain))->GetSocket()->FlushInput( );
}

}
}

UpdateBrains();
}


Program received signal SIGSEGV, Segmentation fault.
0x0805adc2 in IBrainHuman::GetSocket (this=0x0) at src/IBrainHuman.cpp:136
136 {

(gdb) bt
#0 0x0805adc2 in IBrainHuman::GetSocket (this=0x0) at src/IBrainHuman.cpp:136
#1 0x0805f20b in IWorld::HandleInput (this=0x8074940) at src/IWorld.cpp:348


So.. can I not cast from an iterator pointer? Or am I missing something completely obvious.

It will segfault

I should probably take a break, been staring at code for days my brains all over the place
11 Nov, 2010, David Haley wrote in the 2nd comment:
Votes: 0
A NULL pointer from a dynamic cast means that the dynamic cast failed.

You should use gdb to find out the actual type of the *brain object.

Also, make sure that IBrainHuman inherits from IBrain.
11 Nov, 2010, balakoth wrote in the 3rd comment:
Votes: 0
Thanks for teh response, ill jump into gdb and see what I find.

IBrainHuman does derive from IBrain for what its worth heh
11 Nov, 2010, Kaz wrote in the 4th comment:
Votes: 0
if ( (dynamic_cast<IBrainHuman*>(*brain))->GotInput() == true )


The result of dynamic_cast<T*> is either an initialised T* or a NULL pointer. Hence, in this instance, if you submit to the dynamic_cast a brain that wasn't an IBrainHuman* (say, it was an IBrainMobile, likely like most of the things in the brain list), then the ->GotInput() will be calling a member function on a NULL object. Hence your crash. This is almost certainly the case; you can see by the this=0x0 in your stack trace.

You want something like:

IBrainHuman* human_brain = dynamic_cast<IBrainHuman*>(*brain);

if (human_brain && human_brain->GotInput())
{
human_brain->HandleInput(human_brain->GetSocket()->GetInput());
human_brain->GetSocket()->FlushInput();
}
11 Nov, 2010, David Haley wrote in the 5th comment:
Votes: 0
That said, if that were the only issue here, you are making some other mistake somewhere, because you do some of your own home-grown type-checking such that you only try the dynamic cast if you believe it's a human brain. This is why I did not suggest what Kaz did; I think you need to look into this problem beyond the symptom. You think it's a human brain from GetType() but it somehow isn't.
11 Nov, 2010, Kaz wrote in the 6th comment:
Votes: 0
Good point. The brain is clearly a valid object – the call to GetType() worked. However, it isn't an IBrainHuman*, even though it thinks it is.
11 Nov, 2010, balakoth wrote in the 7th comment:
Votes: 0
Well it ended up me being an idiot, and for some reason deleting an iterator somewhere between Input and Descriptor ;) Looked at it and realized it was making the dynamic cast until it got towards the middle of the code (and afte rI had deleted the pointer) Told ya i needed to take a break! But thanks for the help
0.0/7