Re: global NSMutableDictionary
Re: global NSMutableDictionary
- Subject: Re: global NSMutableDictionary
- From: "Michael Ash" <email@hidden>
- Date: Wed, 30 Jul 2008 06:44:26 -0400
On Tue, Jul 29, 2008 at 10:07 PM, Greg Parker <email@hidden> wrote:
> 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.
Thanks for all the info. I'm still a bit confused on one point,
though. You say that if the class has a +load method or C++
constructors then it may not be ready. You also say that libraries you
link to are ready to use before my code runs. Are +load and
constructors exceptions to this rule, or are +load and constructors
only a potential problem if used in my own application code?
Mike
_______________________________________________
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