15

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!

steventnorris
  • 5,656
  • 23
  • 93
  • 174
  • I was having the same issue with a Mac app I am building. Mine was crashing on the third time around, no matter what happened. You're let stupidHack method worked for me. So bizarre, but thanks! – Michael Campsall Nov 10 '14 at 22:33

4 Answers4

8

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.

whitneyland
  • 10,632
  • 9
  • 60
  • 68
  • It works fine in the simulator, but not on a physical device (IPhone 6). – steventnorris Nov 07 '14 at 21:26
  • This issue only happens in release mode, very strange – Zipme Nov 11 '14 at 09:54
  • This does appear to be some sort of bug. However, the let hack I posted above does rectify the issue. My assumption is that it deals with mutability of dictionaries. The let may somehow trigger the dictionary to be mutable again after initialization. – steventnorris Nov 12 '14 at 15:56
  • Has this been reported to Apple/been fixed in Swift 1.2 that anyone knows? – MLowijs Feb 17 '15 at 10:36
  • This bug can be fixed in the newest XCode by changing the Optimization Level under 'Swift Compiler - Code Generation' to 'None'. 'Fastest' seems to be what causes the compiler bug. – steventnorris Mar 19 '15 at 16:19
1

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.

Frederic Adda
  • 5,905
  • 4
  • 56
  • 71
1

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.

r3nar
  • 46
  • 2
0

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.

Ankit
  • 141
  • 1
  • 3