Re: dynamic_cast problem

From: Howard (alicebt_at_hotmail.com)
Date: 07/02/04


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



Relevant Pages

  • Re: unable to call public function in SDI MainFrame from View
    ... To access the function on your derived class, ... > will need to cast the pointer to a pointer to your CMainFrame class. ... > be the case in your MFC app. ... > frame will be a ChildFrame? ...
    (microsoft.public.vc.mfc)
  • Re: unable to call public function in SDI MainFrame from View
    ... To access the function on your derived class, ... will need to cast the pointer to a pointer to your CMainFrame class. ... be the case in your MFC app. ... frame will be a ChildFrame? ...
    (microsoft.public.vc.mfc)
  • Re: Virtual Data?
    ... > pointer to function. ... type of data for a member variable of the same name, ... But your derived class needs a more complicated variable x, ... virtual Init(const int initX); ...
    (comp.lang.cpp)
  • Re: CDialogBar Error with ntdll.dll
    ... > exactly that on a fresh MDI MFC app then it works fine. ... > call the new class (my newly derived class from a new dialog). ... >> CDialogBar is a base class that does not know the dialog template ID. ...
    (microsoft.public.vc.mfc.docview)
  • dynamic_cast problem
    ... I am having problems calling dynamic_caston a pointer returned by a ... The library contains a base class and a derived class and also an extern "C" ... function that returns a pointer ... class Destination_tib: public DestinationImpl { ...
    (comp.lang.cpp)