C++ 5.x: Runtime Problems When Calling Virtual Functions Or Using Dynamic Cast (Doc ID 1334298.1)

Last updated on AUGUST 03, 2016

Applies to:

Oracle Solaris Studio - Version Forte Developer 6 Update 2 to 12.4 [Release 6.0 to 12]
Oracle Solaris on x86 (32-bit)
Oracle Solaris on SPARC (64-bit)
Oracle Solaris on x86-64 (64-bit)
Oracle Solaris on SPARC (32-bit)

Symptoms

In a complex class hierarchy where multiple inheritance is used, one or several of the following symptoms are observed in a C++ application compiled with Sun Studio or Oracle Solaris Studio:

Changes

The problem can occur only if the following conditions are all met:

  1. C++ 5.x on Solaris in default -compat=5 mode
  2. Multiple inheritance is used where
  3. One or more classes or struct inherits from a virtual base class that contains no data member.
  4. The virtual base class that contains no data member is NOT found first in a depth-first left-to-right scan of the inheritance tree. In other words, it is NOT at the bottom of the left-most branch.

Example basic class hierarchy where the problem might be observed:

class VB { // used as a virtual base class
   public:
      // no data members, like this:
      virtual void foo() = 0;
      virtual ~VB() { }
   };


class B : public virtual V { ... };
class C : public virtual V { ... };
class D : public B, public C { ... };

D inherits from the virtual base class V twice, once through B, and once through C:

       V
     /   \
    B     C
     \   /
       D


Basic example code where the wrong virtual function is called:

% cat empty_virtual.cpp
#include <stdio.h>

struct A
{
   virtual void foo( void ) { printf("foo = correct\n"); }
};

struct B
{
   virtual void bar() { printf("bar = ERROR\n"); }
};

struct A1 : public virtual A {};
struct C : public B, public A1 { };
struct C1 : public C { };

int main( int argc, char *argv[] )
{
   A *a = new C1;
   a->foo();

   return 0;
}
% CC empty_virtual.cpp
% ./a.out
bar = ERROR

Cause

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms