Re: global NSMutableDictionary
Re: global NSMutableDictionary
- Subject: Re: global NSMutableDictionary
- From: Greg Parker <email@hidden>
- Date: Tue, 29 Jul 2008 19:07:16 -0700
Michael Ash wrote:
On Tue, Jul 29, 2008 at 2:04 PM, Bill Bumgarner <email@hidden> 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@hidden Runtime Wrangler
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden