Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Make sure you read the discussion on this question/answer, too. <a href="https://stackoverflow.com/questions/20895214/why-should-we-separate-alloc-and-init-calls-to-avoid-deadlocks-in-objective-c/20895427#20895427">Why should we separate alloc and init calls to avoid deadlocks in Objective-C?</a></p> <hr/> <p>To expand on the race condition issue; the <em>real</em> fix is to not have indeterminate initialization within your application. <em>Indeterminate</em> or <em>lazy</em> initialization results in behavior that can easily change due to seemingly innocuous changes -- configuration, "unrelated" code changes, etc...</p> <p>Better to explicitly initialize subsystems on a known-good point in the program's lifespan. I.e. drop <code>[MyClass sharedInstance];</code> into your App delegate's <code>applicationDidFinishLaunching:</code> method if you <em>really</em> need that subsystem initialized early in the program (or move it even earlier, if you want to be extra defensive).</p> <p>Better still to move initialization out of that method entirely. I.e. <code>[MyClass initializeSharedInstance];</code> where <code>+sharedInstance</code> asserts() if that method isn't called first.</p> <p>As much as I'm a a fan of convenience, 25 years of ObjC programming has taught me that lazy initialization is a source of more maintenance and refactoring headaches than it is worth.</p> <hr/> <p><strong>While the race condition described below exists, this code doesn't fix what is described below. It did for a couple of decades when we didn't worry about concurrency in shared instance initializers. Leaving the wrong code for prosperity.</strong></p> <p>Keep in mind that for both Colin's and Harald's otherwise correct answers, there is a very subtle race condition that could lead you to a world of woe.</p> <p>Namely, if the <code>-init</code> of the class being allocated happens to call the <code>sharedInstance</code> method, it will do so before the variable is set. In both cases it will lead to a deadlock.</p> <p>This is the one time that you want to separate the alloc and the init. Cribbing Colin's code because it is the best solution (assuming Mac OS X):</p> <pre><code>+(MyClass *)sharedInstance { static MyClass *sharedInstance = nil; static dispatch_once_t pred; // partial fix for the "new" concurrency issue if (sharedInstance) return sharedInstance; // partial because it means that +sharedInstance *may* return an un-initialized instance // this is from https://stackoverflow.com/questions/20895214/why-should-we-separate-alloc-and-init-calls-to-avoid-deadlocks-in-objective-c/20895427#20895427 dispatch_once(&amp;pred, ^{ sharedInstance = [MyClass alloc]; sharedInstance = [sharedInstance init]; }); return sharedInstance; } </code></pre> <p><strong>note</strong> this only works on Mac OS X; X 10.6+ and iOS 4.0+, in particular. On older operating systems, where blocks are not available, use a lock or one of the various means of doing something once that isn't blocks based.</p> <hr/> <p>The above pattern does not actually prevent the problem described in the text and will cause a deadlock when it is encountered. The problem is that the <code>dispatch_once()</code> is not re-entrant and, thus, if the <code>init</code> calls <code>sharedInstance</code>, <em>wedge city</em>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload