我反对,它正在使用委托(delegate) block 。我正在使用 ARC。
@implementation ViewController
- (void)createGame
{
_game = [[MRCircusGame alloc] init];
_game.mainView = self.view;
_game.stageObjectsDictionary = [self getStageObjectsDictionary];
[_game prepareGame];
}
- (NSMutableDictionary *)getStageObjectsDictionary
{
StrongMan *strongMan = [[StrongMan alloc] initAndCreateImageViewWithFrame:kStrongManIntroFrame inView:self.view];
strongMan.isTakingTouches = NO;
strongMan.isVisible = NO;
[tempDictionary setObject:strongMan forKey:kMRCircusStrongMan];
return tempDictionary;
}
@interface MRCircusGame
{
@property (nonatomic, strong) NSMutableDictionary *stageObjectsDictionary;
}
@implementation StrongMan
..
-(void)method
{
__weak typeof (self) weak_self = self;
self.animator.didEndAnimating = ^{
StrongMan *strongRef = weak_self;
strongRef.isAnimating = NO;
[strongRef idle];
};
}
一些问题:
如果我不使用对 self 的弱引用,Xcode 会发出一些关于可能的保留周期的警告。但我查看了 Instruments,即使使用 self.isAnimating = YES;
如果我不使用这个strongRef解决方法,有时我会得到BAD_EXCESS,所以我认为weak_self已被释放,对吗?我应该一直使用 StrongRef 来防止我的应用程序崩溃吗?
当该 block 执行结束时,strongRef 会被释放吗?
最佳答案
对弱引用进行强引用是在 block 运行时保持自身存活的常用技术。在 block 结束时,strongRef 将被释放。
只有在以下情况下仪器才会显示无法访问的对象
- 您在 block 内使用了对 self 的强引用
- 并且您不会在 dealloc 中清零对 block 的引用
- 并且您取消了对 StrongMan 的引用
关于iphone - block 中对 self 的弱引用有时会导致 BAD_EXCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16263052/