I have attached a UIGestureRecognizer to a UIView. Whose responsibility is it to release this during dealloc?
Specifically:
UITapGestureRecognizer *t =
[[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(tapHandler:)];
[self.view addGestureRecognizer:t];
[t release];
So, self.view currently has sole retention of the gestureRecognizer.
Update I should have been clearer. My question has to do with the views dealloc method. Does the view's superclass handle release of the gestureRecognizer when the view is released. I currently assume that is the case.
The rule of thumb is that you call release
whenever you call alloc
, new
or copy
.
Since you called alloc, your code is not over-releasing or leaking anything.
While you could autorelease your gesture recognizer, I would not because explicitly releasing objects, where possible, is better memory management. (Autoreleased objects don't get released until the autorelease pool is drained.)
Your code is correct.
The view takes ownership of the gesture recoginzer with [self.view addGestureRecognizer:t]
.
You could tidy your code by autoreleasing t
when you create it:
UITapGestureRecognizer *t = [[[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(tapHandler:)] autorelease];
[self.view addGestureRecognizer:t];
This would mean that all ownership of t
is handled in one place thus reducing the potential for introducing problems if the code gets modifed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With