解决的办法就是使用一个__weak变量取代: __weak DetailViewController *weakSelf = self; self.animatedView.block = ^( CGContextRef context, CGRect rect, CFTimeInterval totalTime, CFTimeInterval deltaTime) { DetailViewController *strongSelf = weakSelf; if (strongSelf != nil ) { CGPoint textPoint = CGPointMake((rect. size.width - textSize.width )/2, (rect.size.height - textSize.height)/2); [strongSelf. artistName drawAtPoint:textPoint withFont :font]; } }; 块外的弱引用不增加retainCount. 块内的强引用退出块时会被释放。 如果单纯的在块内引用外部的self,会增加retainCount,而不会释放。 如果这个 block 被异步调用那么创建一个 强引用保持这个被使用的对象处于活动状态是正确的。 DetailViewController 会在退出块后retainCount=0,才被释放。 释放被延迟了。 autoreleasepool 的一个优点。 |
|