• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Catching C++ exceptions thrown from a framework
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Catching C++ exceptions thrown from a framework


  • Subject: Re: Catching C++ exceptions thrown from a framework
  • From: Steve Baxter <email@hidden>
  • Date: Tue, 18 Jul 2006 15:23:31 +0100

Hi Chris,

Hmm, odd that DTS didn't mention this when I asked them specifically about this problem!

Does -fvisibility-ms-compat force RTTI comparison to happen by type name rather than type ID address (i.e. the same class implemented in two different plugins will be considered to be the same for RTTI even though the typeid for the types will have different addresses)?

How does it do this without changing the C++ runtime (I believe the problem here is in the runtime as installed by the OS rather than in Xcode)?

Cheers,

Steve.

On 18 Jul 2006, at 14:22, Chris Espinosa wrote:

On Jul 18, 2006, at 2:03 AM, Steve Baxter wrote:

This has bitten a few people.

The actual problem is that GCC 4 uses the *address* of typeids when comparing for RTTI rather than the class name that is in the typeid. This is arguably more efficient, but doesn't work properly if your code doesn't obey the one definition rule:

http://en.wikipedia.org/wiki/One_Definition_Rule

Unfortunately it is very easy to accidentally not obey the ODR. Inline code used in plugins will cause RTTI information to be linked multiple times (once in each plugin). The compiler and linker will not catch problems here - you have to deal with this yourself.

To fix the problem:

- Your plugins need to export all their symbols

- If you use CFBundle to load your plugins, it will not do the right thing. You need to load your plugin with dlopen ( RTLD_GLOBAL ) (you can then load it with CFBundle as well)

- If you are supporting 10.3.9 you have an extra problem - there is a bug in the C++ runtime which will still prevent RTTI from working between modules. I hope Apple is going to release a fix for this at some point (the fix is actually in the 10.3.9 and 10.4u SDKs)

Doing this allows the dynamic linker to merge symbols as each plugin loads. Each symbol will then be loaded only once and ODR will be obeyed.

Personally I think it is a complete pain for GCC to have different behaviour to CodeWarrior and VC++ in this respect. They have extracted a little bit of performance (comparing the address of typeids rather than doing a text compare is more efficient) but caused an awful lot of pain. Apple could fix this - there is a #define in the C++ runtime to switch back to string comparisons. Unfortunately with 10.3.9 and upwards, this is part of the system and Apple are refusing to change it.

Cheers,

Steve.

On 14 Jul 2006, at 22:14, Eric Slosser wrote:

Sam, this is a semi-well-known problem.

GCC decides to give an exception to a handler when the typeinfo of the exception matches that of the handler (or is a sub- class). But the comparsion isn't based on the munged name of the symbol, it's based on the equality of a integer constant that's handled out by the linker.

So you need to export the typeinfo symbols representing the exceptions you care about from both the catcher and catchee.

The easiest way to see if this is your problem is to export all symbols, and don't strip any symbols, and see if the problem goes away.

If this helps you, thank Marshal Clow for reading your problem and remembering that I had the same one months ago. Oh, and Lisa Lippencott and Sean Parent for explaining the answer to me back then. And the academy. And mom.

See http://www.devworld.apple.com/documentation/Darwin/Reference/ ManPages/man1/gcc.1.html, especially this part:


-fvisibility-ms-compat
This flag attempts to use visibility settings to make GCC's C++
linkage model compatible with that of Microsoft Visual Studio.


           The flag makes these changes to GCC's linkage model:

1. It sets the default visibility to 'hidden', like - fvisibil-
ity=hidden. 2. Types, but not their members, are not hidden by
default. 3. The One Definition Rule is relaxed for types without
explicit visibility specifications which are defined in more than
one different shared object: those declarations are permitted if
they would have been permitted when this option was not used.


This option is discouraged, rather, it is preferable for types to
be explicitly exported as desired on a per-class basis. Unfortu-
nately because Visual Studio can't compare two different hidden
types as unequal for the purposes of type_info and exception han-
dling, users are able to write code that relies upon this behavior.


Among the consequences of these changes are that static data mem-
bers of the same type with the same name but defined in different
shared objects will be different, so changing one will not change
the other; and that pointers to function members defined in differ-
ent shared objects will not compare equal. When this flag is
given, it is a violation of the ODR to define types with the same
name differently.


You can pass this flag manually in the Other C++ Flags build setting in Xcode 2.3 and later.

Chris

Steve Baxter Software Development Manager Improvision +44-2476-692229

_______________________________________________
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


  • Follow-Ups:
    • Re: Catching C++ exceptions thrown from a framework
      • From: Howard Hinnant <email@hidden>
References: 
 >Re: Catching C++ exceptions thrown from a framework (From: Eric Slosser <email@hidden>)
 >Re: Catching C++ exceptions thrown from a framework (From: Steve Baxter <email@hidden>)

  • Prev by Date: Re: Text macro specifications submenu
  • Next by Date: Re: [ANN] Xcode + Leopard at WWDC this year
  • Previous by thread: Re: Catching C++ exceptions thrown from a framework
  • Next by thread: Re: Catching C++ exceptions thrown from a framework
  • Index(es):
    • Date
    • Thread