开发者

What is causing a nil object at objects[0] when access to object is synchronized?

We're getting the following exception when we call [self.sessions allValues]. Anywhere we are using sessions, we are also syncing on the lock o开发者_如何学Cbject 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];
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜