添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Im getting a rather baffling error when trying to get an object from an NSDictionary . I get an EXC_BAD_ACCESS when calling objectForKey . When I look at the given objects, everything is fine. The dictionary exists. It contains the key I am looking for. The key is also there. Everything is totally fine. So why does objectForKey crash on this occasion? Ive taken a screenshot of both the code and of the console - you can see the properties in question in the print out. The key is there, and the dictionary contains that key. But its as though the dictionary doesnt exist or something? Baffled. By the way I 'copy'd the dictionary in an attempt to fix the crash, I wouldnt normally do that.

self.downloadProgress property is an NSDictionary , ok. What is the property ownership attribute? I guess it is not copy , right? Evgeny Karkan Jul 27, 2016 at 15:22 Look closely to log - you have an __NSDictionaryM - which is mutable instance. Pretty unexpected right? So check where it becomes mutable and fix it. Evgeny Karkan Jul 27, 2016 at 15:31 To fix it just add copy attribute like this: @property (nonatomic, copy) NSDictionary *downloadProgress; Hope it will help. Evgeny Karkan Jul 27, 2016 at 15:39 It's probably worth finding out exactly what is causing the segfault. Try using the p command (instead of po ) on some things and finding which thing prints out the same address as shown in the error message. tbodt Jul 27, 2016 at 17:40

The problem here was todo with threading. As I was correctly reminded in comments, NSMutableDictionary is not thread safe. It was being updated on a background thread, and the code above was being called on the main thread. The simplest solution was to wrap these calls in @synchronised. Thanks to all those who chipped in to help.

if ([update uniqueId]) {
    @synchronized (self.downloadProgress) {
        if ([self.downloadProgress objectForKey:[update uniqueId]]) {
            NSDictionary *progressInfo = [[self.downloadProgress objectForKey:[update uniqueId]] copy];
            if ([progressInfo objectForKey:@"progressString"]) {
                return [progressInfo objectForKey:@"progressString"];
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.