help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: dynamic_cast problem


From: Ulrich Eckhardt
Subject: Re: dynamic_cast problem
Date: Thu, 06 Jul 2006 22:16:22 +0200
User-agent: KNode/0.10.2

Earl Purple wrote:
> I have a model whereby I dynamically load libraries with dlopen then
> verify that they are the correct types with dynamic_cast.

You can't do that. dlsym() returns a void pointer and that is not something
you can safely use dynamic_cast on. If it works, it works because the
types are right, if they aren't it might as well crash. The right cast is
a simple static_cast.

Other than that, there is a known bug that dynamic_cast with objects from
shared libraries doesn't work because it compares the type information
addresses(!) and those are different because both the main program and the
library has a copy of it, but I can't tell you exactly which versions of
GCC are affected nor if it really is a GCC error and not a problem with a
particular system.

> It is vital I use dynamic_cast because reinterpret_cast would of course
> lead to undefined behaviour if the type is a mismatch.

reinterpret_cast it probably wrong anyway, because it is only guaranteed to
return the original value when it was formerly reinterpret_casted to
another type. This difference is purely academic though. A simple
static_cast is the right thing probably.

> And because the name of the symbol to be loaded is read from a
> configuration file, there is no possibility of compile-time checks.
> 
> Effectively, is it impossible to do object-broking in this manner using
> gcc?

Not safely. There are simply too many things that could go wrong or that a
malicious plugin could do that it is really hard to check. Why not take a
component model in the first place if you need these checks?

Uli
-- 
http://gcc.gnu.org/faq.html
http://parashift.com/c++-faq-lite/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]