By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,678 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,678 IT Pros & Developers. It's quick & easy.

[Q] Strange dynamic_cast problem

P: n/a
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_boo...ter15_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.

Nov 4 '08 #1
Share this Question
Share on Google+
18 Replies


P: n/a

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

Nov 4 '08 #2

P: n/a
On Nov 4, 2:18*pm, sean_in_rale...@yahoo.com wrote:
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.
Nov 4 '08 #3

P: n/a
On Nov 4, 2:18*pm, sean_in_rale...@yahoo.com wrote:
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.
Nov 4 '08 #4

P: n/a
On Nov 4, 11:58*am, Eric <ericg...@gmail.comwrote:
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_boo...s/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.
Nov 4 '08 #5

P: n/a
On Nov 4, 2:40*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 4, 11:58*am, Eric <ericg...@gmail.comwrote:
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.

thank you for your comments.
Nov 4 '08 #6

P: n/a
On Nov 4, 2:40*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 4, 11:58*am, Eric <ericg...@gmail.comwrote:
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.

thank you for your comments.
Nov 4 '08 #7

P: n/a
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.
Nov 4 '08 #8

P: n/a
On Nov 4, 4:42*pm, Noah Roberts <u...@example.netwrote:
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 thenUB.

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.
Nov 4 '08 #9

P: n/a
On Nov 4, 4:42*pm, Noah Roberts <u...@example.netwrote:
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 thenUB.

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.
Nov 4 '08 #10

P: n/a
On Nov 4, 5:01*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 4, 3:40*pm, Eric <ericg...@gmail.comwrote:
On Nov 4, 2:40*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 4, 11:58*am, Eric <ericg...@gmail.comwrote:
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 does seem like the type is known and yet the dynamic_cast

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

is failing.

Nov 4 '08 #11

P: n/a
On Nov 4, 5:01*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 4, 3:40*pm, Eric <ericg...@gmail.comwrote:
On Nov 4, 2:40*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 4, 11:58*am, Eric <ericg...@gmail.comwrote:
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.

Nov 4 '08 #12

P: n/a
On Nov 4, 8:40 pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 4, 11:58 am, Eric <ericg...@gmail.comwrote:
[...]
// 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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 5 '08 #13

P: n/a
On Nov 4, 10:46 pm, Eric <ericg...@gmail.comwrote:
On Nov 4, 4:42 pm, Noah Roberts <u...@example.netwrote:
[...]
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*.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 5 '08 #14

P: n/a
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 );
Nov 5 '08 #15

P: n/a
On Nov 4, 3:17*pm, Eric <ericg...@gmail.comwrote:
On Nov 4, 5:01*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 4, 3:40*pm, Eric <ericg...@gmail.comwrote:
On Nov 4, 2:40*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 4, 11:58*am, Eric <ericg...@gmail.comwrote:
Ok...this seems to be treading into some really esoteric areas ofc++.
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 downto
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 takesa
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).

I am not sure if this is just a g++ issue or a general C++ issue.
Nov 5 '08 #16

P: n/a
On Nov 5, 8:12*am, Kamil Grabowski <grabowski.ka...@gmail.comwrote:
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.

Nov 5 '08 #17

P: n/a
On Nov 5, 10:07*am, Daniel <drus...@gmail.comwrote:
On Nov 4, 3:17*pm, Eric <ericg...@gmail.comwrote:
On Nov 4, 5:01*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 4, 3:40*pm, Eric <ericg...@gmail.comwrote:
On Nov 4, 2:40*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 4, 11:58*am, Eric <ericg...@gmail.comwrote:
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 mentionedin
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.
-----


Nov 5 '08 #18

P: n/a
On Nov 5, 7:10*am, James Kanze <james.ka...@gmail.comwrote:
On Nov 4, 10:46 pm, Eric <ericg...@gmail.comwrote:
On Nov 4, 4:42 pm, Noah Roberts <u...@example.netwrote:

* *[...]
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.

Nov 5 '08 #19

This discussion thread is closed

Replies have been disabled for this discussion.