<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: +initialize Can Be Executed Multiple Times (+load not so much)</title>
	<atom:link href="http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/</link>
	<description>...so google can index my head.</description>
	<lastBuildDate>Fri, 10 Feb 2012 00:09:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Quora</title>
		<link>http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/comment-page-1/#comment-198323</link>
		<dc:creator>Quora</dc:creator>
		<pubDate>Thu, 01 Sep 2011 17:50:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.friday.com/bbum/?p=1523#comment-198323</guid>
		<description>&lt;strong&gt;What are some useful categories for iOS development?...&lt;/strong&gt;

Answering the question with a warning... Be really careful with categories. While absolutely awesome (and one of the Objective-C features I love the most) categories can trip you up and cause your code to be cruftier and harder to maintain. * Up until ...</description>
		<content:encoded><![CDATA[<p><strong>What are some useful categories for iOS development?&#8230;</strong></p>
<p>Answering the question with a warning&#8230; Be really careful with categories. While absolutely awesome (and one of the Objective-C features I love the most) categories can trip you up and cause your code to be cruftier and harder to maintain. * Up until &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brent</title>
		<link>http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/comment-page-1/#comment-194344</link>
		<dc:creator>Brent</dc:creator>
		<pubDate>Fri, 14 May 2010 11:45:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.friday.com/bbum/?p=1523#comment-194344</guid>
		<description>Note that in non-gc, +load runs without an autorelease pool, and so you have to make your own, which is ugly. But all one-time initialization code ends up ugly to some degree, I guess. You&#039;re always checking whether you&#039;ve already done it, and you have to make sure you handle synchronization.

Sometimes it&#039;s best to just take shared data/code and put it into a different class, probably a custom singleton.

I like the pattern of having a class method to retrieve this stuff; let it do what it has to do lazily and handle synchronization. Alternately, make sure you deliberately get your initialization out of the way before you go spawning threads. Granted if you&#039;re writing a library, you won&#039;t have control over that. I personally like to avoid any kind of cyclical dependency between classes if at all possible, and stick with hierarchies; they&#039;re easier to understand. I don&#039;t so much like Bill&#039;s turning a rule-of-thumb into a flat-out commandment, but whether or not you can (or should) do heavy lifting in +initialize, there&#039;s probably no overwhelmingly convincing reason to do so.</description>
		<content:encoded><![CDATA[<p>Note that in non-gc, +load runs without an autorelease pool, and so you have to make your own, which is ugly. But all one-time initialization code ends up ugly to some degree, I guess. You&#8217;re always checking whether you&#8217;ve already done it, and you have to make sure you handle synchronization.</p>
<p>Sometimes it&#8217;s best to just take shared data/code and put it into a different class, probably a custom singleton.</p>
<p>I like the pattern of having a class method to retrieve this stuff; let it do what it has to do lazily and handle synchronization. Alternately, make sure you deliberately get your initialization out of the way before you go spawning threads. Granted if you&#8217;re writing a library, you won&#8217;t have control over that. I personally like to avoid any kind of cyclical dependency between classes if at all possible, and stick with hierarchies; they&#8217;re easier to understand. I don&#8217;t so much like Bill&#8217;s turning a rule-of-thumb into a flat-out commandment, but whether or not you can (or should) do heavy lifting in +initialize, there&#8217;s probably no overwhelmingly convincing reason to do so.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bbum&#39;s weblog-o-mat &#187; Blog Archive &#187; objc_msgSend() Tour Part 4: Method Lookup &#38; Some Odds and Ends</title>
		<link>http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/comment-page-1/#comment-193055</link>
		<dc:creator>bbum&#39;s weblog-o-mat &#187; Blog Archive &#187; objc_msgSend() Tour Part 4: Method Lookup &#38; Some Odds and Ends</dc:creator>
		<pubDate>Thu, 04 Feb 2010 17:22:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.friday.com/bbum/?p=1523#comment-193055</guid>
		<description>[...] that during +initialize, methods won&#8217;t always be cached. Yet another reason to not do any real work during +initialize!In part 3, the cache lookup loop contained a NULL check and, if NULL was encountered in [...]</description>
		<content:encoded><![CDATA[<p>[...] that during +initialize, methods won&#8217;t always be cached. Yet another reason to not do any real work during +initialize!In part 3, the cache lookup loop contained a NULL check and, if NULL was encountered in [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sas</title>
		<link>http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/comment-page-1/#comment-191880</link>
		<dc:creator>sas</dc:creator>
		<pubDate>Wed, 07 Oct 2009 09:28:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.friday.com/bbum/?p=1523#comment-191880</guid>
		<description>I think your (very interesting argument) resembles a bit Scott Myers&#039; Effective C++ Item 47: Use multiple inheritance judiciously. Yes, you can make MI work if you&#039;re careful but invariably someone down the line will do something that will cause trouble, because your initial assumptions don&#039;t apply anymore. If I understand correctly, even though the locking can be designed to properly work around the ordering issue, at some point (maybe much later) someone ignorant of that design could accidentally introduce potential dead-locking. With the additional danger that it might not even cause problems straight away (the worst kind, really). So in that sense doing heavy lifting in +initialize is fragile I guess.</description>
		<content:encoded><![CDATA[<p>I think your (very interesting argument) resembles a bit Scott Myers&#8217; Effective C++ Item 47: Use multiple inheritance judiciously. Yes, you can make MI work if you&#8217;re careful but invariably someone down the line will do something that will cause trouble, because your initial assumptions don&#8217;t apply anymore. If I understand correctly, even though the locking can be designed to properly work around the ordering issue, at some point (maybe much later) someone ignorant of that design could accidentally introduce potential dead-locking. With the additional danger that it might not even cause problems straight away (the worst kind, really). So in that sense doing heavy lifting in +initialize is fragile I guess.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bbum</title>
		<link>http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/comment-page-1/#comment-191565</link>
		<dc:creator>bbum</dc:creator>
		<pubDate>Sat, 12 Sep 2009 17:52:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.friday.com/bbum/?p=1523#comment-191565</guid>
		<description>James -- you are correct in that you can jump through hoops to design and implement your code such that it can work in the unpredictable, unordered, world of +initialize invocation.  And my code, as stated, was a contrived example -- it was a simple single threaded example of the multithreaded pattern that leads to deadlock.

That your code demonstrates that it is possible neither makes it correct, nor a good idea.

The bottom line is that +initializes are executed in arbitrary order and there are locks involved.  If you cause complex sub-systems to be initialized from +initialize, your code will be fragile.  It might work on your system, but not on a system with a larger/smaller number of cores.  It might work on 10.5.8, but it might not on 10.6.0.  Or it might break between 10.6.1 and 10.6.2.   You are setting your code up to be dependent upon the +initialize of ever other class in your runtime being equally as well behaved and ordered as your code.

As I have said before, I have debugged many many situations where deadlocks have occurred because the developer was surprised by a sudden change in order of calls to +initialize or had naively introduced concurrency without realizing the subtle dependencies incurred by +initializes own locking implementation.   In all cases, fixing the code moved the heavy lifting out of +initialize and into a much more controlled phase of application bringup.

The resulting code was less fragile, easier to maintain, and often eliminated various bits of locking infrastructure since the initialization work was triggered by passing through a very well known and controlled phase in application bringup.

There are much better phases in application launch or subsystem initialization to do this kind of work.  

&lt;b&gt;Do not do heavy lifting in +initialize.  It is fragile, prone to concurrency bugs that are difficult to debug, and, in the near future, the documentation will emphatically indicate that you shouldn&#039;t do so.&lt;/b&gt;</description>
		<content:encoded><![CDATA[<p>James &#8212; you are correct in that you can jump through hoops to design and implement your code such that it can work in the unpredictable, unordered, world of +initialize invocation.  And my code, as stated, was a contrived example &#8212; it was a simple single threaded example of the multithreaded pattern that leads to deadlock.</p>
<p>That your code demonstrates that it is possible neither makes it correct, nor a good idea.</p>
<p>The bottom line is that +initializes are executed in arbitrary order and there are locks involved.  If you cause complex sub-systems to be initialized from +initialize, your code will be fragile.  It might work on your system, but not on a system with a larger/smaller number of cores.  It might work on 10.5.8, but it might not on 10.6.0.  Or it might break between 10.6.1 and 10.6.2.   You are setting your code up to be dependent upon the +initialize of ever other class in your runtime being equally as well behaved and ordered as your code.</p>
<p>As I have said before, I have debugged many many situations where deadlocks have occurred because the developer was surprised by a sudden change in order of calls to +initialize or had naively introduced concurrency without realizing the subtle dependencies incurred by +initializes own locking implementation.   In all cases, fixing the code moved the heavy lifting out of +initialize and into a much more controlled phase of application bringup.</p>
<p>The resulting code was less fragile, easier to maintain, and often eliminated various bits of locking infrastructure since the initialization work was triggered by passing through a very well known and controlled phase in application bringup.</p>
<p>There are much better phases in application launch or subsystem initialization to do this kind of work.  </p>
<p><b>Do not do heavy lifting in +initialize.  It is fragile, prone to concurrency bugs that are difficult to debug, and, in the near future, the documentation will emphatically indicate that you shouldn&#8217;t do so.</b></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/comment-page-1/#comment-191563</link>
		<dc:creator>James</dc:creator>
		<pubDate>Sat, 12 Sep 2009 16:31:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.friday.com/bbum/?p=1523#comment-191563</guid>
		<description>bbum,

Thank you so much for updating your post to make my point for me. :)

Your code example is brilliant and illustrates my original comment perfectly. The problem here is in the implementation of the thread synchronization and has nothing to do with +initialize. Specifically, the use of a single NSLock in this design is fatally flawed. There are multiple methods using the same NSLock that can call each other recursively. That&#039;s just wrong. Either they should be using separate locks, or an NSRecursiveLock, as appropriate.

Bad code can&#039;t be use as an inditement against +initialize. You could add this code to any other set of method, class or instance, and have the same problem. The code example that should have been supplied should look something like this:

(note: code pasted into browser from a working Xcode project - might not run as copied)

&lt;pre&gt;
@interface TestRunner : NSObject
+ (void)testThread:(id)ignored;
@end

@interface OneClass : NSObject
- (void)hello;
@end

@interface TwoClass : NSObject
@end

NSRecursiveLock *globalResourceLock = nil;
static OneClass *oneCircular;
static TwoClass *twoCircular;

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSLog(@&quot;Hi!&quot;);
	
	globalResourceLock = [NSRecursiveLock new];
	for (int i=0; i&lt;3; i++)
		[NSThread detachNewThreadSelector:@selector(testThread:)
								 toTarget:[TestRunner class]
							   withObject:nil];
	
	[NSThread sleepForTimeInterval:4.0];
	[globalResourceLock release];
	NSLog(@&quot;Bye!&quot;);
	
    [pool drain];
    return 0;
}

@implementation TestRunner

+ (void)testThread:(id)ignored
{
	NSAutoreleasePool *pool = [NSAutoreleasePool new];
	
	NSLog(@&quot;Thread starting...&quot;);
	OneClass *test = [[[OneClass alloc] init] autorelease];
	[test hello];
	
	[pool release];
}

@end

@implementation OneClass

+(void)initialize
{
	NSLog(@&quot;%s started&quot;,__func__);
	[globalResourceLock lock];
	twoCircular = [TwoClass new];
	[NSThread sleepForTimeInterval:2.0];
	[globalResourceLock unlock];
	NSLog(@&quot;%s done&quot;,__func__);
}

- (void)hello
{
	NSLog(@&quot;%@ says hello&quot;,self);
}

@end

@implementation TwoClass

+(void)initialize {
	NSLog(@&quot;%s started&quot;,__func__);
	[globalResourceLock lock];
	oneCircular = [OneClass new];
	[globalResourceLock unlock];
	NSLog(@&quot;%s done&quot;,__func__);
}
@end
&lt;/pre&gt;

Here&#039;s an application the spans multiple threads, started currently, that all initialize the same class, which initializes another class, that recursively uses the first class. This should be the perfect storm for +initialize to lock up our application. But, of course, it runs perfectly:

&lt;pre&gt;
2009-09-12 09:19:20.798 InitializeTest[2180:10b] Hi!
2009-09-12 09:19:20.803 InitializeTest[2180:1403] Thread starting...
2009-09-12 09:19:20.803 InitializeTest[2180:1503] Thread starting...
2009-09-12 09:19:20.803 InitializeTest[2180:1603] Thread starting...
2009-09-12 09:19:20.804 InitializeTest[2180:1403] +[OneClass initialize] started
2009-09-12 09:19:20.805 InitializeTest[2180:1403] +[TwoClass initialize] started
2009-09-12 09:19:20.806 InitializeTest[2180:1403] +[TwoClass initialize] done
2009-09-12 09:19:22.806 InitializeTest[2180:1403] +[OneClass initialize] done
2009-09-12 09:19:22.808 InitializeTest[2180:1403]  says hello
2009-09-12 09:19:22.809 InitializeTest[2180:1503]  says hello
2009-09-12 09:19:22.809 InitializeTest[2180:1603]  says hello
2009-09-12 09:19:24.803 InitializeTest[2180:10b] Bye!
&lt;/pre&gt;

So I return to my original assertion: If +initialize is locking up your application, then your thread synchronization is flawed. Solving the problem by avoiding +initialize is simply avoiding the bug in your code. It doesn&#039;t fix the real problem, and the problem has nothing to do with +initialize.</description>
		<content:encoded><![CDATA[<p>bbum,</p>
<p>Thank you so much for updating your post to make my point for me. <img src='http://www.friday.com/bbum/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Your code example is brilliant and illustrates my original comment perfectly. The problem here is in the implementation of the thread synchronization and has nothing to do with +initialize. Specifically, the use of a single NSLock in this design is fatally flawed. There are multiple methods using the same NSLock that can call each other recursively. That&#8217;s just wrong. Either they should be using separate locks, or an NSRecursiveLock, as appropriate.</p>
<p>Bad code can&#8217;t be use as an inditement against +initialize. You could add this code to any other set of method, class or instance, and have the same problem. The code example that should have been supplied should look something like this:</p>
<p>(note: code pasted into browser from a working Xcode project &#8211; might not run as copied)</p>
<pre>
@interface TestRunner : NSObject
+ (void)testThread:(id)ignored;
@end

@interface OneClass : NSObject
- (void)hello;
@end

@interface TwoClass : NSObject
@end

NSRecursiveLock *globalResourceLock = nil;
static OneClass *oneCircular;
static TwoClass *twoCircular;

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSLog(@"Hi!");

	globalResourceLock = [NSRecursiveLock new];
	for (int i=0; i&lt;3; i++)
		[NSThread detachNewThreadSelector:@selector(testThread:)
								 toTarget:[TestRunner class]
							   withObject:nil];

	[NSThread sleepForTimeInterval:4.0];
	[globalResourceLock release];
	NSLog(@&quot;Bye!&quot;);

    [pool drain];
    return 0;
}

@implementation TestRunner

+ (void)testThread:(id)ignored
{
	NSAutoreleasePool *pool = [NSAutoreleasePool new];

	NSLog(@&quot;Thread starting...&quot;);
	OneClass *test = [[[OneClass alloc] init] autorelease];
	[test hello];

	[pool release];
}

@end

@implementation OneClass

+(void)initialize
{
	NSLog(@&quot;%s started&quot;,__func__);
	[globalResourceLock lock];
	twoCircular = [TwoClass new];
	[NSThread sleepForTimeInterval:2.0];
	[globalResourceLock unlock];
	NSLog(@&quot;%s done&quot;,__func__);
}

- (void)hello
{
	NSLog(@&quot;%@ says hello&quot;,self);
}

@end

@implementation TwoClass

+(void)initialize {
	NSLog(@&quot;%s started&quot;,__func__);
	[globalResourceLock lock];
	oneCircular = [OneClass new];
	[globalResourceLock unlock];
	NSLog(@&quot;%s done&quot;,__func__);
}
@end
</pre>
<p>Here&#8217;s an application the spans multiple threads, started currently, that all initialize the same class, which initializes another class, that recursively uses the first class. This should be the perfect storm for +initialize to lock up our application. But, of course, it runs perfectly:</p>
<pre>
2009-09-12 09:19:20.798 InitializeTest[2180:10b] Hi!
2009-09-12 09:19:20.803 InitializeTest[2180:1403] Thread starting...
2009-09-12 09:19:20.803 InitializeTest[2180:1503] Thread starting...
2009-09-12 09:19:20.803 InitializeTest[2180:1603] Thread starting...
2009-09-12 09:19:20.804 InitializeTest[2180:1403] +[OneClass initialize] started
2009-09-12 09:19:20.805 InitializeTest[2180:1403] +[TwoClass initialize] started
2009-09-12 09:19:20.806 InitializeTest[2180:1403] +[TwoClass initialize] done
2009-09-12 09:19:22.806 InitializeTest[2180:1403] +[OneClass initialize] done
2009-09-12 09:19:22.808 InitializeTest[2180:1403]  says hello
2009-09-12 09:19:22.809 InitializeTest[2180:1503]  says hello
2009-09-12 09:19:22.809 InitializeTest[2180:1603]  says hello
2009-09-12 09:19:24.803 InitializeTest[2180:10b] Bye!
</pre>
<p>So I return to my original assertion: If +initialize is locking up your application, then your thread synchronization is flawed. Solving the problem by avoiding +initialize is simply avoiding the bug in your code. It doesn&#8217;t fix the real problem, and the problem has nothing to do with +initialize.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: links for 2009-09-10 &#171; Mike&#8217;s Blog</title>
		<link>http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/comment-page-1/#comment-191531</link>
		<dc:creator>links for 2009-09-10 &#171; Mike&#8217;s Blog</dc:creator>
		<pubDate>Thu, 10 Sep 2009 22:02:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.friday.com/bbum/?p=1523#comment-191531</guid>
		<description>[...] bbum’s weblog-o-mat » Blog Archive » +initialize Can Be Executed Multiple Times (+load not so mu... if a subclass does not implement +initialize but its superclass does, then that superclass’s +initialize will be invoked once per non-implementing subclass and once for itself. (tags: iphonedev development objective-c osx cocoa) [...]</description>
		<content:encoded><![CDATA[<p>[...] bbum’s weblog-o-mat » Blog Archive » +initialize Can Be Executed Multiple Times (+load not so mu&#8230; if a subclass does not implement +initialize but its superclass does, then that superclass’s +initialize will be invoked once per non-implementing subclass and once for itself. (tags: iphonedev development objective-c osx cocoa) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: links for 2009-09-10 &#124; /dev/random</title>
		<link>http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/comment-page-1/#comment-191524</link>
		<dc:creator>links for 2009-09-10 &#124; /dev/random</dc:creator>
		<pubDate>Thu, 10 Sep 2009 18:01:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.friday.com/bbum/?p=1523#comment-191524</guid>
		<description>[...] bbum’s weblog-o-mat » Blog Archive » +initialize Can Be Executed Multiple Times (+load not so mu... if a subclass does not implement +initialize but its superclass does, then that superclass’s +initialize will be invoked once per non-implementing subclass and once for itself. (tags: iphonedev development objective-c osx cocoa) [...]</description>
		<content:encoded><![CDATA[<p>[...] bbum’s weblog-o-mat » Blog Archive » +initialize Can Be Executed Multiple Times (+load not so mu&#8230; if a subclass does not implement +initialize but its superclass does, then that superclass’s +initialize will be invoked once per non-implementing subclass and once for itself. (tags: iphonedev development objective-c osx cocoa) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bbum</title>
		<link>http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/comment-page-1/#comment-191523</link>
		<dc:creator>bbum</dc:creator>
		<pubDate>Thu, 10 Sep 2009 17:49:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.friday.com/bbum/?p=1523#comment-191523</guid>
		<description>Documentation clarification is tracked in rdar://problem/7212642.  If that is addressed, you&#039;ll be officially wrong, too. ;)

I didn&#039;t suggest +load should be used.  I stated the opposite, actually.

The issue is one of fragility.  The example I gave is, as stated, contrived, but it is exactly the kind of pattern I have encountered time and again when finding and fixing bugs across many applications.  In all cases, the &quot;heavy lifting&quot; was initializing some random sub-system that ended up spawning threads and/or making network connections.   Totally bad design, for sure, but that wasn&#039;t how the code started -- it evolved into doing that as features were added to the subsystem.

The developer is far better off limiting +initialize to simple global state initialization and doing any sub-system initialization either at launch (if always needed) or on load of whatever needs said sub-system.

Therein lies the problem.  +initialize starts out simple, doesn&#039;t do heavy lifting, but then requires more and more stuff to be integrated into it as soon as you stop just doing simple stuff.</description>
		<content:encoded><![CDATA[<p>Documentation clarification is tracked in rdar://problem/7212642.  If that is addressed, you&#8217;ll be officially wrong, too. <img src='http://www.friday.com/bbum/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I didn&#8217;t suggest +load should be used.  I stated the opposite, actually.</p>
<p>The issue is one of fragility.  The example I gave is, as stated, contrived, but it is exactly the kind of pattern I have encountered time and again when finding and fixing bugs across many applications.  In all cases, the &#8220;heavy lifting&#8221; was initializing some random sub-system that ended up spawning threads and/or making network connections.   Totally bad design, for sure, but that wasn&#8217;t how the code started &#8212; it evolved into doing that as features were added to the subsystem.</p>
<p>The developer is far better off limiting +initialize to simple global state initialization and doing any sub-system initialization either at launch (if always needed) or on load of whatever needs said sub-system.</p>
<p>Therein lies the problem.  +initialize starts out simple, doesn&#8217;t do heavy lifting, but then requires more and more stuff to be integrated into it as soon as you stop just doing simple stuff.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/comment-page-1/#comment-191522</link>
		<dc:creator>James</dc:creator>
		<pubDate>Thu, 10 Sep 2009 17:17:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.friday.com/bbum/?p=1523#comment-191522</guid>
		<description>Oh, how wrong you are! ;)

You may have caused deadlock issues using non-reentrant locks or through some other path, but the +initialize method is wrapped by a recursive lock that will not deadlock through circular references. Thus, class One&#039;s +initialize can invoke class Two&#039;s +initialize, which can safely use class One without any problems. Sample code would be easy to produce.

If you&#039;ve encountered deadlocks in your +initialization code, then it&#039;s due to a flaw in your thread synchronization design. It is not inherently a flaw in how +initialize is designed or implemented.

Your suggestion of using +load is far more dangerous and undesirable. Running code at +load, you may interact with classes that are not yet initialized. And worse, you push initialization code to application load time which simply slows launch times, rather that pushing time consuming initialization to +initialization time (or later) where it produces more efficient and responsive applications.

I&#039;ve used +initialize effectively for many years and have never had a problem with it. I continue to recommend +initialize in all my books as the best solution to lazily initialize static/global class data structures.</description>
		<content:encoded><![CDATA[<p>Oh, how wrong you are! <img src='http://www.friday.com/bbum/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>You may have caused deadlock issues using non-reentrant locks or through some other path, but the +initialize method is wrapped by a recursive lock that will not deadlock through circular references. Thus, class One&#8217;s +initialize can invoke class Two&#8217;s +initialize, which can safely use class One without any problems. Sample code would be easy to produce.</p>
<p>If you&#8217;ve encountered deadlocks in your +initialization code, then it&#8217;s due to a flaw in your thread synchronization design. It is not inherently a flaw in how +initialize is designed or implemented.</p>
<p>Your suggestion of using +load is far more dangerous and undesirable. Running code at +load, you may interact with classes that are not yet initialized. And worse, you push initialization code to application load time which simply slows launch times, rather that pushing time consuming initialization to +initialization time (or later) where it produces more efficient and responsive applications.</p>
<p>I&#8217;ve used +initialize effectively for many years and have never had a problem with it. I continue to recommend +initialize in all my books as the best solution to lazily initialize static/global class data structures.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

