On Jun 11, 2008, at 2:55 PM, Doug Hill wrote:
I'm seeing a strange crash in a virtual C++ method call. I tried looking this up in the mailing list archives to see if anyone else reported this problem but didn't find anything. So I thought I'd throw this out to the Xcode community.
The method crashes because an input parameter passed to it comes in as a bad pointer. Its pointer value is 4 less than the actual passed in value.
I've played around with your sample a little and can make it misbehave by tossing in multiple inheritance. It sounds like something similar may be happening in your app, as you mention going from c-style casts to dynamic casts. You should *never* be doing c-style casts of C++ objects, especially when you have complex inheritance hierarchies. I am guessing that the bug *does* exist under Windows, but just isn't showing up due to variations in the compiler. In any case, here is a sample based on the one you posted that shows the problem of using C-casts with multiple inheritance (and virtual functions getting confused). It doesn't crash, but you could imagine it crashing in a more complex environment. Also, do make sure RTTI is enabled, though if it wasn't I think the dynamic_casts<> would be crashing, so it sounds like it is on.
#include <iostream>
class Base1
{
public:
virtual void Method1( void ) = 0;
};
class Base2
{
public:
virtual void Method2( Base1* inParam ) = 0;
};
class Mixin
{
public:
virtual void Foo(void) { std::cout << "Foo called on Mixin" << std::endl; }
};
class Derived1 : public Mixin, public Base1
{
public:
virtual void Method1( void ) { std::cout << "Method1 on Derived1 called" << std::endl;}
};
class Derived2 : public Base2
{
public:
virtual void Method2( Base1* inParam ) { inParam-> Method1(); }
};
void Test( void )
{
Mixin* m1 = new Derived1();
Base1* bp1;
Base2* bp2 = new Derived2();
bp1 = (Base1*)m1;
bp2->Method2( bp1 );
bp1 = dynamic_cast<Base1*>(m1);
bp2->Method2( bp1 );
}
int main (int argc, char * const argv[]) {
// insert code here...
Test();
return 0;
}