Michael Ash wrote:
On Tue, Jul 29, 2008 at 2:04 PM, Bill Bumgarner <[EMAIL PROTECTED]> wrote:
Or you could annotate a function as a constructor. It will run before main(). static void __InitializeGlobalStuffMan(void) __attribute__ ((constructor));
void __InitializeGlobalStuffMan(void) {
       myGlobalDictionary = [[NSMutableDictionary alloc] init];
       ....
}


Is this safe to do? I would be afraid that this might execute before
the ObjC runtime is initialized, leading to pain and possibly hilarity
when the objc_msgSends execute.

It's safe, usually.

The runtime always goes first. You can't run code that uses a class before the runtime sets up that class. However, if the class has +load methods or C/C++ constructors of its own, then it may be possible to use the class before it is ready for you.

The initialization order is complicated, but in general:
1. All libraries you link to are ready for use before any of your code runs. 2. All of your own classes are prepared by the runtime before any of your code runs. 3. All of your +load methods are called before any of your C/C++ constructors.
4. Superclass +load always runs before subclass +load.

It is possible for one of your constructors or +load methods to use another one of your classes before its own +load is called, so be careful if you have complicated constructor work or constructor work in more than one place.


--
Greg Parker     [EMAIL PROTECTED]     Runtime Wrangler


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to