title: Addsubview and dealloc for memory leaks (retainCount)
body: <p>I am check the retainCount for the subview after add to the view. the code is :</p>
  
  <pre><code>- (void) loadView{
      //...
      toolbar = [[UIToolbar alloc] initWithFrame:nil];
      [[self view] addSubView:toolbar];
  }
  
  - (void) dealloc{
      NSLog(@"count=%d", [toolbar retainCount]);   // count=2
      [toolbar removeFromSuperView];
      NSLog(@"count=%d", [toolbar retainCount]);   // count=1
      [toolbar release]
      NSLog(@"count=%d", [toolbar retainCount]);   // count=1
      toolbar = nil;
      NSLog(@"count=%d", [toolbar retainCount]);   // count=0
  }
  </code></pre>
  
  <p>From this code in the dealloc{}, I have some questions:</p>
  
  <p>1, the first log retainCount for toolbar is 2, because the toolbar after init and added to the self view, the retaincount will become to 2.</p>
  
  <p>2, After the toolbar removeFromSuperView, the retainCount will become to 1.</p>
  
  <p>3, After the toolbar call the release method, the retainCount is still 1, it can not become 0. In my options, because the super view is remain the toolbar(the self view is not release), so the toolbar can not reduse to 0.</p>
  
  <p>4, If call the toolbar set to nil ,the retainCount will be 0. This log is useless.</p>
  
  <p>My questions is:</p>
  
  <p>a)the toolbar call the removeFromSuperView and the release API, the result is the same, the toolbar retainCount will only become to 1. So If i test the code only use each of them, the result is same. So, can I conclude user can only call each one API is OK?</p>
  
  <p>b)From the apple document, the subview is added to the self view, the self view has remain the toolbar handle, so in the dealloc method, if the viewDidUnload is not called, the toolbar retainCount can not be reduce to 0. If the memory is low, the system will reduce the unuseless view and call the viewDidUnload method, it will automatic reduce the toolbar retainCount to 0. So in the dealloc method, should I set the toolbar to nil. I am confuse if I set the toolbar to nil, when the viewDidUnload method is called, the toolbar will be reduced or not? Is there any momory leak?</p>
  
  <p>Thx.</p>