• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Crash in virtual method call
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Crash in virtual method call


  • Subject: Re: Crash in virtual method call
  • From: Brady Duga <email@hidden>
  • Date: Thu, 12 Jun 2008 10:11:32 -0700


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;
}


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Crash in virtual method call
      • From: Paul Walmsley <email@hidden>
References: 
 >Crash in virtual method call (From: Doug Hill <email@hidden>)

  • Prev by Date: Re: Compiler error when handling C++ std::string class variable
  • Next by Date: Re: Crash in virtual method call
  • Previous by thread: Re: Crash in virtual method call
  • Next by thread: Re: Crash in virtual method call
  • Index(es):
    • Date
    • Thread