i’m new to obj-c and i was just wondering if someone could explain this. Here we add a UIGestureRecognizer to the current view, and then we release it right away, why? If we release it will this not make it useless in the program?
recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
((UIPanGestureRecognizer *)recognizer).minimumNumberOfTouches = 3;
[self.view addGestureRecognizer:recognizer];
[recognizer release];
When you execute:
the gesture recognizer retain count is increased. So, the following
releasejust counter-balances the alloc/init action, which returns an object with retain count of 1.In other words, you can think in these terms:
since you want that
self.viewbe the only owner of the gesture recognizer, you callreleaseonce to get things back in order. Whenself.viewis deallocated, it will call release on its gesture recognizer and this will be deallocated as well.If you did not do like that, you would have a memory leak, since when self.view is deallocated,
releasewould be called on the gesture recognizer but this would not be enough to make the retain count drop to zero (because of the initialretainimplied byalloc).Hope this clarifies things a bit.