title: UIView isn't released. Calling delegate with self, and using UIView commitAnimations adds retain count
body: <p>I have a view that is added as a subview in a viewcontroller. The subview has delegates methods and the viewcontroller is assinged as its delegate. The subview has a animation and calls a delegate method when the animation is finished.</p>
  
  <p>The problem is that when the viewcontroller is removed from the view by the navigationcontroller the subview isn't deallocated. Probably because it's release count is >0. When the viewcontroller is removed from view before the subview animation finishes the subview tries to call the delegate (which is the viewcontroller which doesn't exist anymore) method and i get a EXC_BAD_ACCESS.</p>
  
  <p>Maybe some sample will clarify things ;):</p>
  
  <p>The view</p>
  
  <pre><code>- (void)somefunction {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(viewDidAnimate:finished:context:)];
    [UIView setAnimationDuration:0.3]; 
    self.frame = CGRectMake(0, 320, 320, 47);
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView commitAnimations];
  }
  
  -(void)viewDidAnimate:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
    if (self.delegate != NULL &amp;&amp; [self.delegate respondsToSelector:@selector(viewWasAnimated:)]) {
      [delegate viewWasAnimated:self];
    }
  }
  </code></pre>
  
  <p>viewcontroller</p>
  
  <pre><code>- (void)viewDidLoad{
    [super viewDidLoad];
    MYView *myview = [[MYView alloc] init];
    myview.delegate = self;
    [self.view addSubview:myview]; 
    [myview release];
  }
  
  - (void)viewWasAnimated:(MYView *)view{
  
  }
  </code></pre>
  
  <p>I found out that after the</p>
  
  <pre><code>[UIView commitAnimations];
  </code></pre>
  
  <p>line the retainCount of the view is 2, and after the </p>
  
  <pre><code>[delegate viewWasAnimated:self];
  </code></pre>
  
  <p>the release count is 3. So this is probably why the view isn't released. I know I am not supposed to look at retain counts but don't know what else to do. </p>