[Q] Strange dynamic_cast problem

E

Eric

Ok...this seems to be treading into some really esoteric areas of c++.
I will do my best to explain this issue, but I don't fully understand
what is happening myself. I am hoping that the problem comes down to
standard c++ stuff and is not specific to Mac OS X compiler&linker.

I have put together a simple test project which can be found at:

http://ericgorr.net/LibraryLoading.zip

which demonstrates the problem.

In Shared.h, there are the definitions of two purely virtual classes -
A & B. B is a subclass of A.

In LibraryA, there is a implementation of class B called BImp. There
is a function called GetA which returns a pointer to an instance of
BImp and returns an A*.

In LibraryB, there is a function called test. This function takes a
void *, which will end up being a function pointer to the GetA
function from LibraryA.

The problem comes in the test function with the line:

B* myB = dynamic_cast<B*> (myA);

The dynamic_cast fails and myB is assigned NULL. This _should not_
fail because myA is an instance of class B.

However, I can make this dynamic_cast succeed, if in main.cp, which is
a part of the Application target, I set:

#define CASE_A 1

which allows

A *myA = functionPtr();
B *myB = dynamic_cast<B*> (myA);

to be executed before the test function from LibraryB is called.

Any idea why this allows it to work?
Any idea why it is failing in the first place?

In main.cp, there are two #define's.

#define CASE_A 0
#define CASE_B 0

If both are set to zero, it will crash. This is simplest crashing
case.
If CASE_A is 1, it will work. Any idea why?
If CASE_B is 1, it will crash and this is the case closest to the real
case I am working with.



Since I started composing this message, I've been looking into the
issue a bit more and thought that if it were possible to make the
destructors pure virtual functions as well, then that would solve the
problem.

Googling for "pure virtual destructors c++", I came across:

http://www.linuxtopia.org/online_books/programming_books/thinking_in_c++/Chapter15_024.html

While it seems strange, apparently this is allowed in C++.

So, if I changed Shared.h to look like:

*****
*****
#define VISIBLE __attribute__ ((visibility("default")))

class VISIBLE A
{
public:
virtual ~A( void ) = 0;
virtual void Func( void ) = 0;
};
A::~A() {}


class VISIBLE B : public A
{
public:
virtual ~B( void ) = 0;
virtual void Func1( void ) = 0;
};
B::~B() {}


extern "C" VISIBLE A *GetA( void );
*****
*****

everything worked in all three cases.

Any comments on this solution? Any reason why this wouldn't be
perfectly valid?

Any thoughts or comments would be appreciated.
 
S

sean_in_raleigh

You're more likely to get a good response
if you post a short, compilable example
that exhibits the problem.

Make it easy for us to help you!

Sean
 
E

Eric

You're more likely to get a good response
if you post a short, compilable example
that exhibits the problem.

Make it easy for us to help you!

This is what:

http://ericgorr.net/LibraryLoading.zip

is supposed to be. I realize that it is Mac specific and that this
will limit the number of people who will be able to help, but there
really isn't much code there at all if you wanted to take a look.

In a different forum, someone pointed out that:

-----
All the C++ specification specifies is what happens in a set of C++
source modules, all linked together to form one application. Nowhere
is it discussed what happens if you have DLL's, shared libraries, or
anything else with respect to the language.
-----

which does actually help. In the 'real' case, this is working just
fine within Visual Studio.

If how this works is basically undefined, there will not be a 'right'
solution - or the 'right' solution will be platform specific. In which
case, I will need to deal exclusively with Apple and their forums.
 
E

Eric

You're more likely to get a good response
if you post a short, compilable example
that exhibits the problem.

Make it easy for us to help you!

This is what:

http://ericgorr.net/LibraryLoading.zip

is supposed to be. I realize that it is Mac specific and that this
will limit the number of people who will be able to help, but there
really isn't much code there at all if you wanted to take a look.

In a different forum, someone pointed out that:

-----
All the C++ specification specifies is what happens in a set of C++
source modules, all linked together to form one application. Nowhere
is it discussed what happens if you have DLL's, shared libraries, or
anything else with respect to the language.
-----

which does actually help. In the 'real' case, this is working just
fine within Visual Studio.

If how this works is basically undefined, there will not be a 'right'
solution - or the 'right' solution will be platform specific. In which
case, I will need to deal exclusively with Apple and their forums.
 
S

Salt_Peter

Ok...this seems to be treading into some really esoteric areas of c++.
I will do my best to explain this issue, but I don't fully understand
what is happening myself. I am hoping that the problem comes down to
standard c++ stuff and is not specific to Mac OS X compiler&linker.

I have put together a simple test project which can be found at:

 http://ericgorr.net/LibraryLoading.zip

which demonstrates the problem.

In Shared.h, there are the definitions of two purely virtual classes -
A & B. B is a subclass of A.

In LibraryA, there is a implementation of class B called BImp. There
is a function called GetA which returns a pointer to an instance of
BImp and returns an A*.

In LibraryB, there is a function called test. This function takes a
void *, which will end up being a function pointer to the GetA
function from LibraryA.

The problem comes in the test function with the line:

        B* myB = dynamic_cast<B*> (myA);

The dynamic_cast fails and myB is assigned NULL. This _should not_
fail because myA is an instance of class B.

prove it:

#include <iostream>
#include <typeinfo>

....
// should print a mangled representation of class B
std::cout << typeid(myA).name() << std::endl;

However, I can make this dynamic_cast succeed, if in main.cp, which is
a part of the Application target, I set:

#define CASE_A 1

which allows

                A *myA = functionPtr();
                B *myB = dynamic_cast<B*> (myA);

to be executed before the test function from LibraryB is called.

Any idea why this allows it to work?
Any idea why it is failing in the first place?

In main.cp, there are two #define's.

#define CASE_A 0
#define CASE_B 0

If both are set to zero, it will crash. This is simplest crashing
case.
If CASE_A is 1, it will work. Any idea why?
If CASE_B is 1, it will crash and this is the case closest to the real
case I am working with.

Since I started composing this message, I've been looking into the
issue a bit more and thought that if it were possible to make the
destructors pure virtual functions as well, then that would solve the
problem.

Googling for "pure virtual destructors c++", I came across:

http://www.linuxtopia.org/online_books/programming_books/thinking_in_...

While it seems strange, apparently this is allowed in C++.

So, if I changed Shared.h to look like:

*****
*****
#define VISIBLE __attribute__ ((visibility("default")))

class VISIBLE A
{
public:
    virtual ~A( void )          = 0;
    virtual void Func( void )   = 0;};

A::~A() {}

class VISIBLE B : public A
{
public:
    virtual ~B( void )          = 0;
    virtual void Func1( void )  = 0;};

B::~B() {}

extern "C" VISIBLE A *GetA( void );
*****
*****

everything worked in all three cases.

Any comments on this solution? Any reason why this wouldn't be
perfectly valid?

Any thoughts or comments would be appreciated.

Whether the destructor is virtual or not won't matter, unless that
d~tor were the only function available. All you need is one virtual
function in a base class.

A dynamic_cast will fail if:

a) the types involved are not polymorphic: base has at least one
virtual member function
b) the compiler is not configured to store RTTI (Real Time Type
Information)
c) the object *at* the dynamic_cast's arguement is a Parent type of
the lhv.

Base base;
Base* p_base = &base;
Derived* p_d = dynamic_cast< Derived* >(p_base); // failure

So the following should run with no errors:

#include <iostream>
#include <stdexcept>

class Base
{
virtual void test() {}
};

class Derived: public Base
{
void test() {}
};

int main ()
{
try
{
Derived derived;
Base* p_base = &derived;
Derived* p_der = dynamic_cast< Derived* >(p_base);
if (p_der == 0)
throw std::runtime_error("failed dynamic_cast< Derived* >");
}
catch(const std::exception& e)
{
std::cout << "Error: ";
std::cout << e.what() << std::endl;
}
}

No need for a virtual d~tor since i'm not allocating and deallocating
manually using Base* anywhere. I say your problem is b), but thats a
dice roll.
 
E

Eric

prove it:

#include <iostream>
#include <typeinfo>

...
// should print a mangled representation of class B
std::cout << typeid(myA).name() << std::endl;

The getMyA function only has a single line of code:

return new BImp;

and BImp is subclass of B which is a subclass of A.

Of course, typeid(myA).name() does not print a mangled representation
of class B (it prints 'P1A'), but this is the problem I am trying to
solve.

I don't know why (for certain) this does not work. Looking at the code
alone and stepping through it with the debugger, it is trivial to see
that it should be working. But when the dynamic_cast it hit, it fails.

I am beginning to think this is an OS specific issue as I mentioned in
a reply to sean.

A dynamic_cast will fail if:

b) the compiler is not configured to store RTTI (Real Time Type
Information)
I say your problem is b), but thats a
dice roll.

RTTI is configured properly.

thank you for your comments.
 
E

Eric

prove it:

#include <iostream>
#include <typeinfo>

...
// should print a mangled representation of class B
std::cout << typeid(myA).name() << std::endl;

The getMyA function only has a single line of code:

return new BImp;

and BImp is subclass of B which is a subclass of A.

Of course, typeid(myA).name() does not print a mangled representation
of class B (it prints 'P1A'), but this is the problem I am trying to
solve.

I don't know why (for certain) this does not work. Looking at the code
alone and stepping through it with the debugger, it is trivial to see
that it should be working. But when the dynamic_cast it hit, it fails.

I am beginning to think this is an OS specific issue as I mentioned in
a reply to sean.

A dynamic_cast will fail if:

b) the compiler is not configured to store RTTI (Real Time Type
Information)
I say your problem is b), but thats a
dice roll.

RTTI is configured properly.

thank you for your comments.
 
N

Noah Roberts

Eric wrote
In LibraryB, there is a function called test. This function takes a
void *, which will end up being a function pointer to the GetA
function from LibraryA.

The problem comes in the test function with the line:

B* myB = dynamic_cast<B*> (myA);

The dynamic_cast fails and myB is assigned NULL. This _should not_
fail because myA is an instance of class B.

A lot of people make the mistake of casting in and out of void* using
different types. This will cause UB. The reason being can be seen in
4.10.2:

The result of converting a "pointer to cv T" to a "pointer to cv void"
points to the start of the storage location where the object of type T
resides, as if the object is a most derived object (1.8) of type T (that
is, not a base class subobject).

In other words, if you cast a B* to a void*, then cast that void* to an
A* (or visa-versa) you're going to have serious problems. The A* will
now, possibly incorrectly, point to a location in memory that is a B*
without the correct interpretation step that would normally transpire in
a static_cast. The result of accessing this pointer in any way is then UB.

If you're using MI you are GOING to have problems here. Otherwise you
may or may not.

I solve this problem in our shop by disallowing the passing of variables
through a void* without a type-safe wrapper like boost::any. This makes
sure that the pointers on both sides of the cast will be the exact same
type, as they must be to avoid UB.
 
E

Eric

Eric wrote





A lot of people make the mistake of casting in and out of void* using
different types.  This will cause UB.  The reason being can be seen in
4.10.2:

The result of converting a "pointer to cv T" to a "pointer to cv void"
points to the start of the storage location where the object of type T
resides, as if the object is a most derived object (1.8) of type T (that
is, not a base class subobject).

In other words, if you cast a B* to a void*, then cast that void* to an
A* (or visa-versa) you're going to have serious problems.  The A* will
now, possibly incorrectly, point to a location in memory that is a B*
without the correct interpretation step that would normally transpire in
a static_cast.  The result of accessing this pointer in any way is then UB.

If you're using MI you are GOING to have problems here.  Otherwise you
may or may not.

I solve this problem in our shop by disallowing the passing of variables
through a void* without a type-safe wrapper like boost::any.  This makes
sure that the pointers on both sides of the cast will be the exact same
type, as they must be to avoid UB.

Just to be clear, none of the variables are ever passing through a
void*.

The only use of a void* in the sample code is to pass around a pointer
to a function. In every case, the function is always successfully
passed and called.

Again, it is unlikely the usage of the void* in the code is the cause
of this problem.

Any other suggestions people have are always appreciated.
 
E

Eric

Eric wrote





A lot of people make the mistake of casting in and out of void* using
different types.  This will cause UB.  The reason being can be seen in
4.10.2:

The result of converting a "pointer to cv T" to a "pointer to cv void"
points to the start of the storage location where the object of type T
resides, as if the object is a most derived object (1.8) of type T (that
is, not a base class subobject).

In other words, if you cast a B* to a void*, then cast that void* to an
A* (or visa-versa) you're going to have serious problems.  The A* will
now, possibly incorrectly, point to a location in memory that is a B*
without the correct interpretation step that would normally transpire in
a static_cast.  The result of accessing this pointer in any way is then UB.

If you're using MI you are GOING to have problems here.  Otherwise you
may or may not.

I solve this problem in our shop by disallowing the passing of variables
through a void* without a type-safe wrapper like boost::any.  This makes
sure that the pointers on both sides of the cast will be the exact same
type, as they must be to avoid UB.

Just to be clear, none of the variables are ever passing through a
void*.

The only use of a void* in the sample code is to pass around a pointer
to a function. In every case, the function is always successfully
passed and called.

Again, it is unlikely the usage of the void* in the code is the cause
of this problem.

Any other suggestions people have are always appreciated.
 
S

Salt_Peter

The getMyA function only has a single line of code:

  return new BImp;

and BImp is subclass of B which is a subclass of A.

Of course, typeid(myA).name() does not print a mangled representation
of class B (it prints 'P1A'), but this is the problem I am trying to
solve.

I don't know why (for certain) this does not work. Looking at the code
alone and stepping through it with the debugger, it is trivial to see
that it should be working. But when the dynamic_cast it hit, it fails.

I am beginning to think this is an OS specific issue as I mentioned in
a reply to sean.



RTTI is configured properly.

Ah, so...

1) you are manually allocating and deallocating with new/delete so
virtual destructors are required in your base class. Not doing so
equates to a memory leak unless you use certain smart_pointers.

2) You stated:

[ The dynamic_cast fails and myB is assigned NULL. This _should not_
fail because myA is an instance of class B. ]

therefore myA is a pointer to an instance of B, should be

typeid( *myA ).name();

and you should be getting a B then.
If you still need help with this issue, construct a short, simple
program:

##include <iostream>
#include <typeinfo>

class A
{
public:
virtual ~A() { std::cout << "~A()\n"; }
};

class B : public A { };

int main ()
{
A* p_a = new B;
std::cout << typeid( *p_a ).name() << std::endl;
delete p_a;

std::cout << "Press ENTER to EXIT.\n";
std::cin.get();
}

/*
1B // type B as expected
~A()
*/

If you make A's destructor non-virtual, you have a memory leak, part
of your BImp doesn't get zapped on delete (test it here, ~A() would
not get invoked).

I never,ever use new/delete unless no other solution is available, in
fact i hate naked pointers like the plague, prefer references.
Something like boost::shared_ptr or even std::auto_ptr is a far better
solution. Or you can store elements in a container like std::vector<>
(instead of new overhere and then delete overthere).

My 3 cents
 
E

Eric

The getMyA function only has a single line of code:
  return new BImp;
and BImp is subclass of B which is a subclass of A.
Of course, typeid(myA).name() does not print a mangled representation
of class B (it prints 'P1A'), but this is the problem I am trying to
solve.
I don't know why (for certain) this does not work. Looking at the code
alone and stepping through it with the debugger, it is trivial to see
that it should be working. But when the dynamic_cast it hit, it fails.
I am beginning to think this is an OS specific issue as I mentioned in
a reply to sean.
RTTI is configured properly.

Ah, so...

1) you are manually allocating and deallocating with new/delete so
virtual destructors are required in your base class. Not doing so
equates to a memory leak unless you use certain smart_pointers.

2) You stated:

[ The dynamic_cast fails and myB is assigned NULL. This _should not_
fail because myA is an instance of class B. ]

therefore myA is a pointer to an instance of B, should be

typeid( *myA ).name();

and you should be getting a B then.

Indeed I do. the output is: 4BImp

So, it does seem like the type is known and yet the dynamic_cast

B* myB = dynamic_cast<B*> (myA);

is failing.
 
E

Eric

The getMyA function only has a single line of code:
  return new BImp;
and BImp is subclass of B which is a subclass of A.
Of course, typeid(myA).name() does not print a mangled representation
of class B (it prints 'P1A'), but this is the problem I am trying to
solve.
I don't know why (for certain) this does not work. Looking at the code
alone and stepping through it with the debugger, it is trivial to see
that it should be working. But when the dynamic_cast it hit, it fails.
I am beginning to think this is an OS specific issue as I mentioned in
a reply to sean.
RTTI is configured properly.

Ah, so...

1) you are manually allocating and deallocating with new/delete so
virtual destructors are required in your base class. Not doing so
equates to a memory leak unless you use certain smart_pointers.

2) You stated:

[ The dynamic_cast fails and myB is assigned NULL. This _should not_
fail because myA is an instance of class B. ]

therefore myA is a pointer to an instance of B, should be

typeid( *myA ).name();

and you should be getting a B then.

Indeed, I do. The output is: 4BImp. So, it would appear the type is
properly known.

And yet, the dynamic_cast is failing.
 
J

James Kanze

On Nov 4, 11:58 am, Eric <[email protected]> wrote:

[...]
// should print a mangled representation of class B
std::cout << typeid(myA).name() << std::endl;

The standard doesn't say what it should print. And from a QoI
point of view, printing the mangled name rates at about the
bottom, just above always returning an empty string. A good
compiler will print the unmangled name.
 
K

Kamil Grabowski

First of all, your code is not ready to be properly built. You need to
add #ifndef/#define/#endif include guards to your .h files. Second,
there is no main module which calls your test() function, so I don't
know how do you actually call it from your code. I also needed to add
declarations to your mainA.h and mainB.h files so I could use the
functions in my main module, and change the VISIBLE definition to an
empty one (my compiler doesn't support __attribute__). Anyway, using
Visual Studio 2008, I wrote this simple test program:

// main.cpp

#include "Shared.h"
#include "mainA.h"
#include "mainB.h"

int main()
{
test( &GetA );
return 0;
}

and it worked fine. BTW you are using an illegal C++ construct, that
is casting a function pointer to a void*. Your test() function should
probably look like this:

extern "C" VISIBLE void test( T* p );
 
D

Daniel

Ah, so...
1) you are manually allocating and deallocating with new/delete so
virtual destructors are required in your base class. Not doing so
equates to a memory leak unless you use certain smart_pointers.
2) You stated:
[ The dynamic_cast fails and myB is assigned NULL. This _should not_
fail because myA is an instance of class B. ]
therefore myA is a pointer to an instance of B, should be
typeid( *myA ).name();
and you should be getting a B then.

Indeed, I do. The output is: 4BImp. So, it would appear the type is
properly known.

And yet, the dynamic_cast is failing.

The problem is most likely that you end up with two definitions of the
type, one in each dynamic library (or one in main and one in the
library). The runtime system can't figure out that the two types are
are the same type, since they have separate v-tables and all that and
so fails to cast. The typical solution is to make sure that at least
one method from the base class is defined in only one dynamic library
(so define the constructor and virtual destructor in a .cpp rather
than the .h).

I am not sure if this is just a g++ issue or a general C++ issue.
 
E

Eric

First of all, your code is not ready to be properly built.

It builds and runs without error in Xcode 3.1.1 if CASE_A = 1.
You need to
add #ifndef/#define/#endif include guards to your .h files.

Normally, I would agree, but such extra stuff isn't necessary in this
test case. If you found it to be necessary, you aren't building it
correctly.
Second,
there is no main module which calls your test() function, so I don't
know how do you actually call it from your code.

The main for the application is found in main.cp.

The application pulls the test function from LibraryB and then passes
it to LibraryA. It is inside of LibraryA that the test function is
called.
I also needed to add
declarations to your mainA.h and mainB.h files so I could use the
functions in my main module, and change the VISIBLE definition to an
empty one (my compiler doesn't support __attribute__).

and it worked fine.

I agree, this would work fine, but it has little to do with the
problem I am attempting to solve.
 
E

Eric

Ok...this seems to be treading into some really esoteric areas of c++.
I will do my best to explain this issue, but I don't fully understand
what is happening myself. I am hoping that the problem comes down to
standard c++ stuff and is not specific to Mac OS X compiler&linker.
I have put together a simple test project which can be found at:
 http://ericgorr.net/LibraryLoading.zip
which demonstrates the problem.
In Shared.h, there are the definitions of two purely virtual classes -
A & B. B is a subclass of A.
In LibraryA, there is a implementation of class B called BImp. There
is a function called GetA which returns a pointer to an instance of
BImp and returns an A*.
In LibraryB, there is a function called test. This function takes a
void *, which will end up being a function pointer to the GetA
function from LibraryA.
The problem comes in the test function with the line:
        B* myB = dynamic_cast<B*> (myA);
The dynamic_cast fails and myB is assigned NULL. This _should not_
fail because myA is an instance of class B.
prove it:
#include <iostream>
#include <typeinfo>
...
// should print a mangled representation of class B
std::cout << typeid(myA).name() << std::endl;
The getMyA function only has a single line of code:
  return new BImp;
and BImp is subclass of B which is a subclass of A.
Of course, typeid(myA).name() does not print a mangled representation
of class B (it prints 'P1A'), but this is the problem I am trying to
solve.
I don't know why (for certain) this does not work. Looking at the code
alone and stepping through it with the debugger, it is trivial to see
that it should be working. But when the dynamic_cast it hit, it fails.
I am beginning to think this is an OS specific issue as I mentioned in
a reply to sean.
A dynamic_cast will fail if:
b) the compiler is not configured to store RTTI (Real Time Type
Information)
I say your problem is b), but thats a
dice roll.
RTTI is configured properly.
Ah, so...
1) you are manually allocating and deallocating with new/delete so
virtual destructors are required in your base class. Not doing so
equates to a memory leak unless you use certain smart_pointers.
2) You stated:
[ The dynamic_cast fails and myB is assigned NULL. This _should not_
fail because myA is an instance of class B. ]
therefore myA is a pointer to an instance of B, should be
typeid( *myA ).name();
and you should be getting a B then.
Indeed, I do. The output is: 4BImp. So, it would appear the type is
properly known.
And yet, the dynamic_cast is failing.

The problem is most likely that you end up with two definitions of the
type, one in each dynamic library (or one in main and one in the
library). The runtime system can't figure out that the two types are
are the same type, since they have separate v-tables and all that and
so fails to cast. The typical solution is to make sure that at least
one method from the base class is defined in only one dynamic library
(so define the constructor and virtual destructor in a .cpp rather
than the .h).

Yes, this is the conclusion I am reaching as well. The question is
what to do about it.

The underlying design being used here is for LibraryA to be a plugin
which provides the implementation for class B. LibraryB is another
plugin which needs to use the functionality provided by LibraryA. So,
Library B needs to know the information from Shared.h, but cannot know
*anything* about the implementation. I believe the problem is that the
destructor issue breaks the abstract purity of class A and class B,
providing some implementation and therefore duplicate definitions that
the dynamic_cast cannot figure out.

Maintaining this kind of separation does not seem possible with your
suggested solution and just using (mostly) straight C++.
I am not sure if this is just a g++ issue or a general C++ issue.

It appears to be OS & compiler specific as this all works when
compiled with Visual Studio.

I am coming to the conclusion issues such as these are why Apple
provided things like their CFPlugIn API. Because, as someone else has
pointed out,

-----
All the C++ specification specifies is what happens in a set of C++
source modules, all linked together to form one application. Nowhere
is it discussed what happens if you have DLL's, shared libraries, or
anything else with respect to the language.
-----
 
E

Eric

On Nov 4, 4:42 pm, Noah Roberts <[email protected]> wrote:

   [...]
The only use of a void* in the sample code is to pass around a
pointer to a function.

Which causes another problem: you can't store a pointer to a
function in a void*.

Well, it works, but would be non-portable code.

However, this isn't the issue I am attempting to deal with at the
moment and in the real case, a void* isn't used. It was just done for
simplicity for this test case and is extremely unlikely to be the
cause of the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top