Re: Problem with dynamic_cast & exceptions using GCC 4?
Re: Problem with dynamic_cast & exceptions using GCC 4?
- Subject: Re: Problem with dynamic_cast & exceptions using GCC 4?
- From: Andreas Grosam <email@hidden>
- Date: Tue, 26 Jul 2005 14:38:49 +0200
On 25.07.2005, at 19:07, Jesper Papmehl wrote:
Hi!
I am have trouble getting dynamic_cast to do the right thing when
compiling with GCC 4.
We use a hierarchy of exceptions (mainly the standard ones in
stdexcept) and in various places in out code, we use dynamic_cast to
convert between exceptions and error codes.
You need to ensure that classes which might be thrown in exceptions are
exported and its RTTI symbols can be accessed across different shared
libraries if the instantiation of the object is in a different DSO
(dynamic shared object) than the occurrence of the catch (respectively
the occurrence of a dynamic_cast operator).
You need to *explicitly* export them, if your target uses build
settings which make all symbols "private extern" by default. Check
these build settings:
Symbols Hidden by Default
Inline Functions Hidden
If they are true (both should be either on or off), all symbols are
"private extern" by default, otherwise they are extern per default -
which is essentially the same as in gcc-3.
"private extern" means, symbols - including RTTI information - are only
visible within the same DSO. "extern" means, they are visible accross
DSOs.
If you have a library which defines a class which can be thrown in an
exception (or is a operand in a dynamic_cast), you should (often you
need to!) explicitly export them adding the visibility attribute to the
class definition - since it is required that RTTI information is
visible accross DSOs.
Please, check the gcc-4 documentation about the visibility feature -
and please read the messages in this list with the subject "Exporting
Symbols...", too.
As a rule of thumb, always export classes which might be thrown in
exceptions, or are used as operand in the dynamic_cast operator. Use
macros to exlicitly define the visibility attribute.
Regards
Andreas
We do this by catch:ing std::exception references, and then try to
dynamic_cast the caught exception to the various exception classes to
see which one it is. This works fine in CodeWarrior and Visual Studio,
and also in Xcode when using GCC 3.3, but not when using GCC 4.
For example, when running the following program:
#include <iostream>
#include <stdexcept>
int main(int argc, char* const argv[]) {
try {
throw std::invalid_argument("");
}
catch (const std::exception& e) {
if (dynamic_cast<const std::invalid_argument*>(&e) != NULL) {
std::cout << "got invalid_argument" << std::endl;
}
else if (dynamic_cast<const std::logic_error*>(&e) != NULL) {
std::cout << "got logic_error" << std::endl;
}
else {
std::cout << "got something else" << std::endl;
}
}
return 0;
}
I get a logic_error instead of an invalid_argument when using GCC 4.
I am using Xcode 2.1 and a freshly created C++ Tool project with the
default settings. (C++ exceptions and RTTI are turned on.)
If I change the compiler version from GCC 4 to GCC 3.3, I get an
invalid_argument instead, which is what I expect.
Does anyone have any idea what is going on? Do I have the wrong
expectation about how this is supposed to work? Or is it maybe a GCC
bug?
TIA
/Jesper Papmehl
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden