473,385 Members | 1,357 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,385 software developers and data experts.

dynamic_cast problem

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
Jul 22 '05 #1
3 2479

"KeithO" <ko***@nildram.co.uk> wrote in message
news:40*********************@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
Jul 22 '05 #2
KeithO wrote:
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
Jul 22 '05 #3
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

"Howard" <al*****@hotmail.com> wrote in message
news:IX*******************@bgtnsc05-news.ops.worldnet.att.net...

"KeithO" <ko***@nildram.co.uk> wrote in message
news:40*********************@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

Jul 22 '05 #4

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?...
9
by: Rob | last post by:
(I am not the one who defined these classes) class _jobject {}; class _jarray : public _jobject {}; typedef _jobject* jobject; typedef _jarray jarray; int main() {
18
by: Eric | last post by:
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.