我是否负责在dealloc中发布UIView的gestureRecognizers? [英] Am I responsible for releasing a UIView's gestureRecognizers in dealloc?
问题描述
我已将UIGestureRecognizer附加到UIView。在dealloc期间释放这个是谁的责任?
I have attached a UIGestureRecognizer to a UIView. Whose responsibility is it to release this during dealloc?
具体来说:
UITapGestureRecognizer *t =
[[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(tapHandler:)];
[self.view addGestureRecognizer:t];
[t release];
因此,self.view目前只保留gestureRecognizer。
So, self.view currently has sole retention of the gestureRecognizer.
更新
我应该更清楚。我的问题与视图dealloc方法有关。视图发布时,视图的超类是否处理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.
推荐答案
经验法则是,无论何时打电话,都要拨打发布
alloc
, new
或 copy
。
The rule of thumb is that you call release
whenever you call alloc
, new
or copy
.
由于你调用了alloc,你的代码不会过度释放或泄漏任何东西。
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.)
这篇关于我是否负责在dealloc中发布UIView的gestureRecognizers?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!