virtual table points wrong :(

Started by
8 comments, last by Leadorn 19 years, 5 months ago
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.
Advertisement
Post code, and your questions will be answered.
daerid@gmail.com
You may also want to try a clean/full rebuild of your project.
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
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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.
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.
That Anonymous Poster was me. There really should be a warning when you attempt to post and aren't logged in, it's really annoying.
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
blame us, we are two working on this. I will smack that bitch up.
thanks for all replies.

This topic is closed to new replies.

Advertisement