6

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.

dugla
  • 12,774
  • 26
  • 88
  • 136

2 Answers2

2

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.

Benedict Cohen
  • 11,912
  • 7
  • 55
  • 67
  • It's not a good habit to call autorelease on all your objects instead of releasing them yourself. – Tieme Sep 20 '12 at 11:05
  • @Tieme I disagree. It keeps all memory management code in one place which improves readability and thus reduces potential for leaks due to forgetting to `release`. The delayed release will not affect the memory high-water mark because the object will persist after this method exits anyway. What are your reason for stating that using autorelease is a not a good habit? – Benedict Cohen Sep 20 '12 at 11:25
  • I agree with you on this example. Here it's fine to call autorelease. But it is still not a good habit to alway autoreleasing your objects cause in the end it will add some overhead to your memory management. – Tieme Sep 20 '12 at 11:53
  • 1
    @Tieme that's premature optimisation. Autorelease simplifies memory management and thus reduces the likely hood of bugs. It is better to use autorelease until you can prove that its use is causing a problem. In my 5 years of working with Objective-C I can think of very few occasion when autorelease was causing a problem (the one that springs to mind were image processing on earlier iOS device). I solve these problems by adding an autorelease pool around the offending code rather than take on the burden of memory managment. – Benedict Cohen Sep 20 '12 at 12:12
  • What about this one for example, google and you'll find a dozen others http://stackoverflow.com/questions/193288/what-is-the-cost-of-using-autorelease-in-cocoa – Tieme Sep 20 '12 at 12:53
  • @Tieme the accepted answer for that question is just the argument which I dismissed as premature optimisation. The advantages of having more readable, more robust code greatly out weight the optimisation arguments. – Benedict Cohen Sep 20 '12 at 13:32
2

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.)

Moshe
  • 57,511
  • 78
  • 272
  • 425