当同步对象的访问时,是什么导致对象[0]处出现 nil 对象?

发布于 2024-11-28 04:08:39 字数 980 浏览 0 评论 0原文

当我们调用 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2024-12-05 04:08:39

一种猜测是:

您的会话字典包含键/值(=会话实例)。将它们添加到字典时,键和值将被保留。如果添加到字典中的会话实例存在内存问题(例如,它意外地过度释放),您可能会得到一个无效的会话实例,最终导致异常。因此,您应该检查会话对象的内存管理。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文