title: How many times do I release an allocated or retained object?
body: <p>I am making an iPhone game. I want to release all objects that have been allocated or retained. In the <code>dealloc</code> function I am releasing all such objects, but then I realized that sometimes I end up releasing objects when they have not been allocated yet. So I figured I need to check if its <code>retainCount</code> is greater than zero or not before I release it.</p>
  
  <p>My question is:</p>
  
  <p>Do I just check if the <code>retainCount</code> is greater than zero and then release it?</p>
  
  <pre><code>if([bg retainCount]!=0)
  {
    [bg release];
  }
  </code></pre>
  
  <p>or</p>
  
  <p>Should I release it as many times as its <code>retainCount</code></p>
  
  <pre><code>while([bg retainCount]!=0)
  {
    [bg release];
  }
  </code></pre>
  
  <p>Thanks for your help!</p>