EXC_BAD_ACCESS on iOS 8.1 with Dictionary

倖福魔咒の 提交于 2019-12-09 03:30:52

问题


I have an object accessible via a static var in a struct (workaround for the lack of class variable support in swift right now), structured like this:

struct Constants{
    static var myObj = MyObject()
}

MyObject has a dictionary in it like so:

class MyObject{
    private var params = Dictionary<String,AnyObject>()

    func addParam(key:String, value:AnyObject){
        params[key] = value
    }
}

Now on the first call to this object for Contants.myObj.addParam("param", value:123) all is well and params has contents ["param":123]. On the second call for Contants.myObj.addParam("param", value:456), I get a EXC_BAD_ACCESS.

Here's the kicker though, this only occurs in iOS 8.1. Also, if I add the line let stupidHack = self.params as the first line of my addParam method, it works fine. My assumption is that it deals with mutability of dictionaries. The let may somehow trigger the dictionary to be mutable again after initialization.

Has anyone else run into this issue before? Any idea on how to fix it?

Thanks!


回答1:


Looks like a compiler bug.

Have you tried switching between Release and Debug then rebuilding? If debug works but not release it can be an indication of a compiler/optimizer bug.

Does it happen in the simulator also?

Your code works for me on iOS 8.1 with XCode 6.1.




回答2:


By chance, do you have an iPhone 6 with 64Gb ? I have one and I had the same issue using Dictionaries twice.

In the news (well the tech news ...), I read that the defective memory modules delivered by Toshiba for precisely this iPhone model could cause incorrect allocations in memory.




回答3:


Try adjusting the Swift compiler optimization level to "None" (Build Settings).

I had a similar issue with a class being deallocated for no apparent reason, it is mostly a compiler bug like Lee said.




回答4:


Faced similar kind of issues with swift code and fixed such issues by disabling swift compiler optimisation in the build settings of the application target.



来源:https://stackoverflow.com/questions/26809986/exc-bad-access-on-ios-8-1-with-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!