我有對象,它使用委託塊。我正在使用ARC。對塊中自我的弱引用有時會導致BAD_EXCESS
@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];
};
}
一些問題:
如果我不使用弱引用自我,讓Xcode的有關可能的保留週期一定的警示作用。但是,我來看看在儀器沒有檢測到memery泄漏甚至我用self.isAnimating = YES;
如果我不使用這個strongRef解決辦法,有時我BAD_EXCESS所以我認爲這weak_self發佈了是嗎?我應該一直使用strongRef來防止我的應用程序崩潰嗎?
當該塊執行結束時,會釋放strongRef嗎?
從您顯示的代碼中獲取EXEC_BAD_ACCESS是不可能的。是的,如果'self'在塊觸發前被釋放,那麼'weak_self'將是'nil','strongRef'也是。但是你只是在'strongRef'上調用方法,並且在Objective-C中調用'nil'上的方法是可以的。你必須在其他地方遇到問題。 – newacct 2013-04-28 20:11:15
此外,您顯示的圖片與您的代碼不符。這是什麼? – newacct 2013-04-28 20:14:42
不應該把塊內的引用反而變爲weak_self自己? (圖片內) – mnl 2014-05-22 11:18:18