• 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: Automagic instantiation of singletons?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Automagic instantiation of singletons?


  • Subject: Re: Automagic instantiation of singletons?
  • From: Chris Hanson <email@hidden>
  • Date: Wed, 23 Nov 2005 00:13:02 -0800

On Nov 22, 2005, at 11:21 PM, Theo Vosse wrote:

I wouldn't know, but if +(void)initialize is called before any other message (as the documentation claims), sending any Objective- C message from a constructor of an object instantiated before main () should result in calling +(void)initialize. Consequently, this must initialize the necessary parts of the run-time system, or it must have been initialized before. If not, the documentation is incorrect (which is what I meant with: I don't know of a guarantee).

When you message a class, that class will receive +initialize before it receives any other message, just as the documentation says. And the Objective-C runtime will be brought up to deal with the messaging etc.


However, that does not mean Cocoa will necessarily be brought up completely too. For example, if you message an Objective-C class in a C++ static constructor, and the corresponding method creates an autoreleased object, it will be leaked and (logged as such) since it was created without any autorelease pool in place. Etc. You need to be very careful writing such load-time code to ensure that you have the proper environment in place for what your code needs to do.

Furthermore, C++ doesn't make many guarantees about the order of static initialization (Scott Ribe's point) so you can't use C++ static constructors effectively to attempt to initialize the Cocoa runtime either. For truly predictable behavior you need to do so in main or something called from it, whether you're just setting up an outer autorelease pool (for a Foundation tool) or whether you're invoking NSApplicationMain (for an AppKit application).

  -- Chris

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Automagic instantiation of singletons?
      • From: Scott Ribe <email@hidden>
References: 
 >Re: Automagic instantiation of singletons? (Christian Brunschen) (From: Scott Ribe <email@hidden>)
 >Re: Automagic instantiation of singletons? (Christian Brunschen) (From: Theo Vosse <email@hidden>)

  • Prev by Date: Mouse click mask how?
  • Next by Date: Re: Automagic instantiation of singletons? (Christian Brunschen)
  • Previous by thread: Re: Automagic instantiation of singletons? (Christian Brunschen)
  • Next by thread: Re: Automagic instantiation of singletons?
  • Index(es):
    • Date
    • Thread