dynamic_cast problem

K

KeithO

I am having problems calling dynamic_cast<> on a pointer returned by a
dynamically loaded library.
The problem seems to be related to the fact that I dynamically load the
library because if I link the app with the library it
works fine.

I am using gcc3.04 compiler on Linux AS2.1
The code works fine with MS VC6.0 and also with SunPro5.3 compilers.
The library contains a base class and a derived class and also an extern "C"
function that returns a pointer
to an instance of the derived class.
I then try to dynamically cast this pointer to the derived class which
fails.

Here is my library.
// dynalib.h
class DestinationImpl {
public:
virtual char * getName();
};
class Destination_tib : public DestinationImpl {
public:
char * getName();
virtual char * getTibName();
};
extern "C" DestinationImpl * getDestinationImpl(void);

// dynalib.cpp
#include "dynalib.h"
char * DestinationImpl::getName() { return "DestinationImpl";}
char * Destination_tib::getName() { return "Destination_tib"; }
char * Destination_tib::getTibName() { return "TibName"; }
extern "C" DestinationImpl * getDestinationImpl(void) {return new
Destination_tib;}

Here is my test app.
#include "dynalib.h"
typedef void * (*MODULE_HANDLE)();
int main() {
void * hdll=dlopen("libdynalib.so", RTLD_LAZY|RTLD_GLOBAL );
MODULE_HANDLE fn = (void *(*)())dlsym(hdll, "getDestinationImpl");
DestinationImpl * pDestImpl;
if (fn) { pDestImpl = (DestinationImpl *)(fn()); }
Destination_tib * p = dynamic_cast<Destination_tib *>(pDestImpl); //
This fails p == 0
}

Here is my makefile
g++3 -g -Wall -shared -fPIC dynalib.cpp -I. -o libdynalib.so
g++3 -g -Wall -I. -L. test.cpp -ldl -o test

Is there some linker option I can specify to make this work?

Thanks in advance.

Keith
 
H

Howard

KeithO said:
I am having problems calling dynamic_cast<> on a pointer returned by a
dynamically loaded library.
The problem seems to be related to the fact that I dynamically load the
library because if I link the app with the library it
works fine.

I am using gcc3.04 compiler on Linux AS2.1
The code works fine with MS VC6.0 and also with SunPro5.3 compilers.
The library contains a base class and a derived class and also an extern "C"
function that returns a pointer
to an instance of the derived class.
I then try to dynamically cast this pointer to the derived class which
fails.

Here is my library.
// dynalib.h
class DestinationImpl {
public:
virtual char * getName();
};
class Destination_tib : public DestinationImpl {
public:
char * getName();
virtual char * getTibName();
};
extern "C" DestinationImpl * getDestinationImpl(void);

// dynalib.cpp
#include "dynalib.h"
char * DestinationImpl::getName() { return "DestinationImpl";}
char * Destination_tib::getName() { return "Destination_tib"; }
char * Destination_tib::getTibName() { return "TibName"; }
extern "C" DestinationImpl * getDestinationImpl(void) {return new
Destination_tib;}

Here is my test app.
#include "dynalib.h"
typedef void * (*MODULE_HANDLE)();
int main() {
void * hdll=dlopen("libdynalib.so", RTLD_LAZY|RTLD_GLOBAL );
MODULE_HANDLE fn = (void *(*)())dlsym(hdll, "getDestinationImpl");
DestinationImpl * pDestImpl;
if (fn) { pDestImpl = (DestinationImpl *)(fn()); }

Are you sure that the library is loading? The following line depends upon
the previous line being executed, but you don't include it in the if
statement. And, you don't check if the handle returned from dlopen is valid
before attempting to use it, either. That function may have failed to load
the library, right? (You also don't initialize pDestImpl to NULL, which is
not generally a good thing if you might later attempt to use it, because it
could be *any* value!) I'd add some code to check that stuff.
Destination_tib * p = dynamic_cast<Destination_tib *>(pDestImpl); //
This fails p == 0
}

-Howard
 
V

Victor Bazarov

KeithO said:
I am having problems calling dynamic_cast<> on a pointer returned by a
dynamically loaded library.
The problem seems to be related to the fact that I dynamically load the
library because if I link the app with the library it
works fine.

I am using gcc3.04 compiler on Linux AS2.1 [...]

Is there some linker option I can specify to make this work?

Sorry, but this is off-topic here. Please visit gnu.g++.help or
comp.os.linux.development.apps.

V
 
K

KeithO

The code I posted is simply to re-create the problem. The library is getting
loaded as other calls are working, it's just the dynamic casts that are
failing. Thanks for you help anyway. I have re-psoted in gnu.g++.help.

Keith
 

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