Sign in to follow this  
Leadorn

virtual table points wrong :(

Recommended Posts

Leadorn    100
Hi I have one depressing problem! When my program calls a function it calls the wrong virtual function. What do I do? To use another compile is not an option. We are using vc71 2003. If it was suppose to call foo it calls foobar, I’m getting depressed. I don’t want to redesign everything because of this. What should we do, anyone had this weird problem before.

Share this post


Link to post
Share on other sites
antareus    576
You might see this happen if different libraries share a common header that has had its object layout changed over the course of time. A few things you can do:

* keep projects in sync
* add new virtual functions to the bottom of the class declaration

Share this post


Link to post
Share on other sites
Leadorn    100
Ive narrowed down the bug. we use static cast to void * at one point on the classes. And when it is casted to base class after being void vtable is fucked.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Leadorn
Ive narrowed down the bug. we use static cast to void * at one point on the classes. And when it is casted to base class after being void vtable is fucked.

That's because what you are doing is not a valid conversion. This is not the fault of the compiler, it's because you are using the language improperly. You are never guaranteed that casting from child pointer to void pointer to base pointer will give you a valid base pointer to your object! This is because a cast from child to base pointer is an operation that can potentially change the pointer value. If you cast from child pointer to void pointer, the application has no way of knowing what it is actually pointing to and how to get from the void pointer to the base pointer! So you're just going to be left with a raw pointer conversion, which is not guaranteed to work, especially if you happen to be using multiple inheritance and are casting to something other than the first non-empty base.

Share this post


Link to post
Share on other sites
Enigma    1410
It's no suprise things aren't working if you static_cast to a void* and then back to a base class. When dealing with inheritance hierarchies compilers may adjust the the pointer depending on whether you have a pointer-to-base or pointer-to-subclass. If you circumvent this by casting as you are then Bad Things Can Happen™. Re-evaluate the code that casts to void*.

Secondly, please read this post, which is stickied at the top of the forum and then edit your last post.

Enigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this