Re: Crash in virtual method call
Re: Crash in virtual method call
- Subject: Re: Crash in virtual method call
- From: Doug Hill <email@hidden>
- Date: Thu, 12 Jun 2008 01:04:05 -0700
Hey Jens,
Thanks for the MI tip. The class I'm working indeed uses Multiple
Inheritance with a moderately complex hierarchy. Here are a few notes:
- When creating a standalone set of projects that attempts to mimic
the class hierarchy of my projects that have problems, I don't get any
crashing behavior. Unfortunately the classes are all artificial; that
is they don't have any real implementation other than cout statements.
- When stepping into the virtual methods of my test code, it still
goes through the "non-virtual thunk" and shows bogus source methods
along the way. But it eventually gets to the correct method and
executes with no problems. This is a strange implementation quirk but
there it is.
In my main project, I tried some different theories about what might
be wrong and made some changes to test those theories. For example, I
noticed that there were many casts from derived* to base* without
dynamic_cast or even static_cast. Adding dynamic_cast had no effect.
I wasn't getting any different behavior with the changes I was making.
I then tried switching the order of base classes in the derived class
declaration. For example, instead of:
class Derived : public Base1, public Base2 { ... };
I do
class Derived : public Base2, public Base1 { ... };
When I do this, the problem goes away! I can call all my methods with
no problem, including every method overridden from all base classes.
Not sure why this would fix the problem but things seem to work now.
I'll have to investigate this more as I really don't like making
changes that should have no effect to get code to work. I'm guessing
there's something else going on but it isn't obvious. The only thing
different about the two base classes is that one is pure virtual and
the other is not. There shouldn't be an order for derived classes so
I'm not inclined to believe I should have to do this. Perhaps a
compiler bug? I really wish I had a standalone test-case to prove it.
FWIW, this code comes from a Windows project that I'm porting, and
compiles and runs just fine using the Visual Studio C++ compiler.
While this doesn't necessarily prove anything it certainly is
interesting. Also, I'm unfortunately not in any position to modify
the class hierarchy as this is cross-platform code, so I have to get
it to work pretty much as is.
Doug Hill
On Jun 11, 2008, at 9:14 PM, Jens Alfke wrote:
On 11 Jun '08, at 2:55 PM, Doug Hill wrote:
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.
Does your real code use multiple inheritance? You can run into
trouble like this with MI, where the 'this' pointer will be offset
by the runtime to point to where a secondary base class's instance
data begins. Doing type-casting of object pointers in the wrong way
can interfere with this and get the wrong offsets applied.
(If this is the case, I can't offer more detailed advice, as I've
always tried to stay as far away as possible from MI in C++ for just
these sorts of reasons...)
—Jens
_______________________________________________
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