The problem

In a rather large C++ project I'm working on at the moment (callis -- which at 17,000 lines has passed xuni for size!), I'm having trouble with multiple inheritance. I've created a ReferenceCounter class to perform memory management, automatically freeing memory when the reference count reaches zero. Naturally, sometimes I want to refer to a derived class with a base class pointer, and register both types of pointers in the ReferenceCounter. This has worked very well so far, but now I've used multiple inheritance.

The inheritance tree looks something like this:
Code:
class Base {
public:
    virtual ~Base() {}
};
class Branch1 : public Base {};
class Branch2 : public Base {};
class Derived : public Branch1, public Branch2 {};
I use code similar to this:
Code:
Derived *derived = new Derived();
ReferenceCounter::add<Derived>(derived);

Base *reference = derived;
ReferenceCounter::add<Base>(reference);
The problem is that, since I'm using multiple inheritance, Derived has two vtables, and casting it to a Base gives me a different address! So ReferenceCounter thinks it's a different memory block, and the program soon crashes (try to free memory twice, usually).

Possible solutions

I guess there are two ways to fix this. Either re-write/redesign the ReferenceCounter or don't use multiple inheritance. The ReferenceCounter is using const void* pointers and uses typeid to remember the type of the original object added to it. I don't really think it can be improved that much, unless there's a way to detect whether a class is multiply-inherited or not.

Why multiple inheritance

Maybe there's a better way of doing this. I'll describe my design; please critique it if you think of a better way.

The code is a simple XML parser (to replace mxml). I have a ResourceElement base class, and subclasses ResourceElementRecursive and ResourceElementData. Then I have MXML and DP (DWK's Parser) implementations of these classes. The DP classes are ResourceElementDP, ResourceElementRecursiveDP, and ResourceElementDataDP.

These last two classes use multiple inheritance. ResourceElementRecursiveDP, for example, inherits from ResourceElementDP (which provides some DP-necessary stuff) and from ResourceElementRecursive (which specifies the interface the class provides).

I can't really think of a better way to do this. This pattern occurs elsewhere in the code, however, so I think there might be a better way. I have Image and TransformedImage, for example, and implementations in ImageSDL and TransformedImageSDL (which should inherit from TransformedImage, for the interface, and ImageSDL, for SDL code).

Maybe I've been using too much Java lately, because it would be a perfect place to use Java interfaces.

P.S. In GDB, I was having trouble casting variables when the type resides in a namespace, but I figured out how to do this. In case anyone's interested, you have to use single quotes around the name, e.g.
Code:
print *('Namespace::SomeDerivedClass' *)instance_of_some_base_class