title: What causes "EXC_BAD_ACCESS" when calling a selector?
body: <p>When a selector is called in Objective-C, and the app crashes, resulting in the debug stack pointing to a selector, what is the cause of this? From what I understand, this happens as a result of a memory error. However, both the object I am sending the command to, and the parameter (an array) are valid.  I can tell because these two objects reveal memory locations as well as other info when moused over in Xcode.</p>
  
  <p>Since the trace ends at this selector, I am at a loss.  I have never seen this error when the stack points to a selector with valid object parameters.</p>
  
  <p>Furthermore, this selector is called frequently.  It always deals with the same types of objects, and there is nothing to suggest abnormal behaviour before my crash.<br>
  This crash is infrequent, happening every 10 minutes or less.  </p>
  
  <p>Since I am a novice programmer, there is likely something wrong with the code inside the selector.  Often I compare object pointers like so:</p>
  
  <pre><code>if (thisObject.pointerToSomeObject == nil) { //do stuff...
  </code></pre>
  
  <p>I am beginning to doubt this is proper Objective-C programming, but then again, the code works most of the time.  Is there a corner case I am not aware of?</p>
  
  <p>Additional Info:
  I am using cocos2d version 1.0.0, Xcode 4.1.  Testing on the iPad simulator.</p>
  
  <p>Any help is appreciated, even links to relevant debugging articles would be helpful.</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Developments:</p>
  
  <p>A default debug thread pointing to a selector, with a bad access error can mean that the bad access is happening within that selector's execution. It is possible to turn on further debugging with NSZombies to find the exact line.</p>
  
  <p>So, after turning on NSZombies, The problem is revealed in greater detail.  (Thanks @Lou Franco for that bad access tutorial)</p>
  
  <p>Now, what would cause:</p>
  
  <pre><code>if ([thisZombie target] == nil)
  </code></pre>
  
  <p>to evaluate NO, but throw a bad access when accessing an integer in <code>[thisZombie target]</code>?
  The <code>target</code> is a pointer to an NSObject (zombified)</p>
  
  <pre><code>else {
  
    int diffx = [thisZombie x] - [[thisZombie target] x];
    //                                          ^^ ----- bad access here
  </code></pre>
  
  <hr>
  
  <p>** EDIT 2 **</p>
  
  <p>There must be something wrong with the syntax, or the short-circuit evaluation:</p>
  
  <pre><code>if (!thisZombie.target &amp;&amp; thisZombie.leader &amp;&amp; !thisZombie.leader.dead)
  </code></pre>
  
  <p>thisZombie.leader.dead stops the thread with:
  " <em>*</em> -[Zombie dead]: message sent to deallocated instance 0x2108f0"</p>
  
  <p>I suppose I should mention that this game has "Zombies" which are not to be confused with the "NSZombie" </p>
  
  <p>So, the program evaluates "thisZombie.target" as "true"
  but when I go to access "thisZombie.target.x" the program ceases execution.</p>
  
  <p>Why?</p>
  
  <hr>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>Hi Everyone,
  I'd like to thank you all again for your helpful comments and suggestions.
  I decided to solve this by completely changing how my zombies follow their leaders.</p>
  
  <p>I still have no Idea what would cause evaluation as described above, but the answer is no longer required.</p>