今天介绍一个和循环引用很相似的容器类的问题.
崩溃堆栈
0 CoreFoundation!__exceptionPreprocess + 0x7c
1 libobjc.A.dylib!objc_exception_throw + 0x34
2 CoreFoundation!__NSFastEnumerationMutationHandler + 0x7c
3 MakeFriends!-[MFTransmitService onServiceDataNotification:transmitData:] [MFTransmitService.m : 52 + 0x14]
4 MakeFriends!-[TransmitCore onServiceDataNotification:] [TransmitCore.m : 74 + 0xc]
5 CoreFoundation!__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 0x10
6 CoreFoundation!_CFXRegistrationPost + 0x18c
7 CoreFoundation!___CFXNotificationPost_block_invoke + 0x38
8 CoreFoundation!-[_CFXNotificationRegistrar find:object:observer:enumerator:] + 0x5dc
9 CoreFoundation!_CFXNotificationPost + 0x174
异常信息如下
07-17 17:28:42.601 Exception Name: NSGenericException
07-17 17:28:42.605 Exception Reason: *** Collection <__NSArrayM: 0x17425ec30> was mutated while being enumerated.
从异常信息上来看是访问一个已经被加锁的, 崩溃的原因在某个小伙伴的提示下, 发现并不是因为加锁了, 而是在基于NSArrayM
NSFastEnumeration
协议的NSArray
在快速遍历的更改时候, 会导致NSFastEnumerationState
中的mutationsPtr
改变了
if (startMutations != *enumState.mutationsPtr) objc_enumerationMutation(l_collection);
当系统执行到这行代码时抛异常了. 但是纵观我的代码, 没有涉及到多线程, 不存在线程安全的问题, 下面看代码
ViewController
@interface ViewController ()
@property (nonatomic, strong) FirstViewController *obj1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.testArrayM = [NSMutableArray array];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.testArrayM addObject:self.obj1];
NSNumber *obj2 = @(2);
NSNumber *obj3 = @(3);
[self.testArrayM addObject:obj2];
[self.testArrayM addObject:obj3];
[CommonData sharedObject].commonData = self.testArrayM;
while (1) {
sleep(10);
[self test1];
}
}
- (void)test1 {
for (NSObject *num in [CommonData sharedObject].commonData) {
[self handleNum:num];
}
}
- (void)handleNum:(NSObject *)num {
_obj1 = nil;
[self.obj1 test1];
}
- (NSObject *)obj1 {
if (_obj1 == nil) {
_obj1 = [[FirstViewController alloc] init];
}
return _obj1;
}
FirstViewController
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)dealloc {
NSLog(@"FirstViewController dealloc");
[[CommonData sharedObject].commonData removeObject:self];
}
- (void)test1 {
NSLog(@"test1");
}
- (instancetype)init {
if (self = [super init]) {
[self initVc];
}
return self;
}
- (void)initVc {
[[CommonData sharedObject].commonData addObject:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
CommonData.h
@interface CommonData : NSObject
+ (instancetype)sharedObject;
@property (nonatomic, strong) NSMutableArray *commonData;
@end
CommonData.m
@implementation CommonData
+ (instancetype)sharedObject {
static dispatch_once_t __once;
static CommonData * __instance = nil;
dispatch_once(&__once, ^{
__instance = [[CommonData alloc] init];
});
return __instance;
}
@end
CommonData
是一个公用的数据存储类, ViewController
对commonData
数组进行遍历, 在遍历的时候又调用了FirstViewController
的init
, init
里面又去操作CommonData
的commonData
, 就造成了对正在遍历的commonData
进行了修改, 而系统在遍历的时候应该是对这个commonData
进行加锁的, 这就造成了访问加锁数据崩溃.
修复方法
修改ViewController中使用方法
- (void)test1 {
NSArray *temp = [[CommonData sharedObject].commonData mutableCopy];
for (NSObject *num in temp) {
[self handleNum:num];
}
}
对commonData
进行复制后, 加锁的就不是commonData而是temp, 这样再其它地方进行修改就没问题了, 这里给我们的教训是, 在遍历muteble容器的时候, 最好进行一次复制, 这样能避免容器在被遍历的时候被修改也不会有问题.
总结
造成崩溃是因为, ViewController->commonData->FirstViewController->commonData, 这样就形成了类似commonData的循环引用. 最终导致崩溃, 解决的方案也是类似破除循环引用的方式, ViewController->temp->FirstViewController->commonData.