Re: dynamic_cast problem
From: Howard (alicebt_at_hotmail.com)
Date: 07/02/04
- Next message: Victor Bazarov: "Re: dynamic_cast problem"
- Previous message: Ioannis Vranos: "Re: re-setting an array"
- In reply to: KeithO: "dynamic_cast problem"
- Next in thread: KeithO: "Re: dynamic_cast problem"
- Reply: KeithO: "Re: dynamic_cast problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 02 Jul 2004 19:45:44 GMT
"KeithO" <kowen@nildram.co.uk> wrote in message
news:40e5b8bb$0$118$65c69314@mercury.nildram.net...
> 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
- Next message: Victor Bazarov: "Re: dynamic_cast problem"
- Previous message: Ioannis Vranos: "Re: re-setting an array"
- In reply to: KeithO: "dynamic_cast problem"
- Next in thread: KeithO: "Re: dynamic_cast problem"
- Reply: KeithO: "Re: dynamic_cast problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|