当同步对象的访问时,是什么导致对象[0]处出现 nil 对象?
当我们调用 [self.sessions allValues]
时,我们遇到以下异常。无论我们在哪里使用会话,我们也会在锁定对象互斥锁上进行同步。什么会导致这个异常?
[NSArray initWithObjects:count:]:尝试在对象[0]处插入nil对象
0 CoreFoundation 0x334ff987 __exceptionPreprocess + 114
1 libobjc.A.dylib 0x331b449d objc_exception_throw + 24
2 CoreFoundation 0x33487bf7 -[__NSPlaceholderArray initWithObjects:count:] + 270
3 CoreFoundation 0x3349730d +[NSArray arrayWithObjects:count:] + 32
4 CoreFoundation 0x334a16e7 -[NSDictionary allValues] + 282
@synchronized (mutex) {
if (!self.sessions) {
return [NSArray array];
}
NSMutableArray* activeSessions = [[NSMutableArray alloc] init];
for (id<AccountSession> session in [self.sessions allValues]) {
if (session) {
[activeSessions addObject:session];
}
}
return [activeSessions autorelease];
}
We're getting the following exception when we call [self.sessions allValues]
. Anywhere we are using sessions, we are also syncing on the lock object mutex. What would cause this exception?
[NSArray initWithObjects:count:]: attempt to insert nil object at objects[0]
0 CoreFoundation 0x334ff987 __exceptionPreprocess + 114
1 libobjc.A.dylib 0x331b449d objc_exception_throw + 24
2 CoreFoundation 0x33487bf7 -[__NSPlaceholderArray initWithObjects:count:] + 270
3 CoreFoundation 0x3349730d +[NSArray arrayWithObjects:count:] + 32
4 CoreFoundation 0x334a16e7 -[NSDictionary allValues] + 282
@synchronized (mutex) {
if (!self.sessions) {
return [NSArray array];
}
NSMutableArray* activeSessions = [[NSMutableArray alloc] init];
for (id<AccountSession> session in [self.sessions allValues]) {
if (session) {
[activeSessions addObject:session];
}
}
return [activeSessions autorelease];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
一种猜测是:
您的会话字典包含键/值(=会话实例)。将它们添加到字典时,键和值将被保留。如果添加到字典中的会话实例存在内存问题(例如,它意外地过度释放),您可能会得到一个无效的会话实例,最终导致异常。因此,您应该检查会话对象的内存管理。
One guess is:
Your session Dictionary contains key/values (=session instances). While adding them to the Dictionary, the keys and values are retained. If there is a memory issue with the session instance added to the dictionary - e.g. it accidently gets overreleased - you could end up with an invalid session instance which finally causes the exception. So you should check your memory management for the session objects.