473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

[Q] Strange dynamic_cast problem

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
18 5254

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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when...
8
by: Thomas Lorenz | last post by:
Hello, first, I didn't find any reference to my question through googling. dynamic_cast uses RTTI to determine if the cast expression is valid. Invalid casts of pointers give a '0'-result. As...
2
by: yccheok | last post by:
Hello, I have an object called XXX previously derived from CDocument in my MDI project. Later, I create an concrete class called Subject. And I let XXX to have multiple inheritance from Subject...
5
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler)....
22
by: Boris | last post by:
I'm porting code from Windows to UNIX and ran into a problem with dynamic_cast. Imagine a class hierarchy with three levels: class Level2 derives from Level1 which derives from Base. If you look...
5
by: mijobee | last post by:
Hello Everyone, I just wanted to check that I'm using dynamic_cast correctly. I have a hierarchy of objects using pure virtual classes and virtual inheritance to implement interfaces. I ran...
15
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to ...
15
by: Bo Yang | last post by:
Hi, I can understand static_cast, reinterpret_cast and const_cast, they all work at compile time. But I can figure out how the C++'s dynamic- cast works? Could you please explain how for me?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.