MI and clone()
I'm having a very weird problem here that's apparently caused by MI
and/or virtual inheritance that I'd like help with. I finally managed
to get the problem to appear in small form. I'll leave the code at the
end so I can explain some things more deeply before hand.
The really interesting thing is that even before the call that causes
the crash, the debugger is having troubles. For example, if I place a
breakpoint in sub_base3::do_x() I am not able to examine *this as a
sub_base3. The debugger thinks it is a sub_base1 and will not allow me
to cast it down in the watch window (even though it breaks in the
correct place and performs the correct output string).
My call stack at the crash looks like this:
scratch.exe!derived2::derived2(const derived2 & __that={...}) +
0x56 bytes C++
scratch.exe!derived2::clone() Line 38 + 0x50 bytes C++
scratch.exe!derived2::clone() + 0x2b bytes C++
scratch.exe!main() Line 61 + 0x22 bytes C++
scratch.exe!__tmainCRTStartup() Line 586 + 0x19 bytes C
scratch.exe!mainCRTStartup() Line 403 C
kernel32.dll!7c817077()
[Frames below may be incorrect and/or missing, no symbols loaded
for kernel32.dll]
Looks like some seriously F'ed up UB to me but I'm not well versed in MI
and virtual inheritance to understand the nuances I'm running into. I
thought I'd done it right.
Also, I don't know if this is meaningful but I got a C4505 for the
sub_base3::clone() = 0 override. First time I've ever seen that warning
and it blows up in my face.
CODE:
#include <string>
#include <iostream>
struct base {
virtual void do_x() { std::cout << "base::do_x()\n"; }
virtual base * clone() const = 0;
virtual int * value() = 0;
virtual ~base() {}
};
struct sub_base1 : virtual base
{
virtual void do_x() { std::cout << "sub_base1::do_x()\n"; }
//virtual sub_base1 * clone() const = 0;
};
struct sub_base2 : virtual base
{
virtual void do_x() { std::cout << "sub_base2::do_x()\n"; }
//virtual sub_base2 * clone() const = 0;
};
struct derived : sub_base1, sub_base2
{
virtual void do_x() { std::cout << "derived::do_x()\n"; }
virtual derived * clone() const { return new derived; }
int * value() { return 0; }
//void do_y() { }
};
struct sub_base3 : sub_base1
{
void do_x()
{
std::cout << "sub_base3::do_x() -> " << *value() << "\n";
}
virtual sub_base3 * clone() const = 0;
};
struct derived2 : sub_base3
{
void do_y() {}
virtual derived2 * clone() const { return new derived2(*this); }
int * value() { return val; }
derived2(int * v) : val(v) {}
private:
int * val;
};
int main()
{
int x = 5;
derived2 d(&x);
d.do_x();
//d.clone()->do_x();
base * b = &d;
b->do_x();
base * b2 = b->clone();
b2->do_x();
sub_base3 * sb3 = dynamic_cast<sub_base3*>(b2);
sb3->do_x();
sub_base3 * sb32 = sb3->clone(); // this call explodes.
sb32->do_x();
std::cin.get();
}
ENDCODE
The reason sub_base3 has a clone() override is that it is called from
code that knows the type is sub_base3 and needs to get a clone pointer
to that level. I've done stuff like that a million times before but not
with MI.
--
http://crazyeddiecpp.blogspot.com