9

I'm using a dictionary to evaluate an expression, when the expression has variables and the dictionary is actually used by NSExpression, something happens and I get EXC_BAD_ACCESS when trying to update the dictionary, this only happens when debugging in an iPhone6, not in the simulator and not in an iPhone 4S.

    let strExpression = "a+b+20"
    let exp = NSExpression(format:strExpression)
    self.dictionary = ["a":10.0, "b":15.0, "c":25.0]
    let value:AnyObject = exp.expressionValueWithObject(self.dictionary, context: nil)
    let doubleValue = value as Double
    self.dictionary.updateValue(doubleValue, forKey: "c")    

Something really weird is that if i add this line just after creating the dictionary, then it woks fine:

let newDic = self.dictionary    

I,m using iOS 8.1. Thanks in advance!

juanelomx
  • 251
  • 3
  • 4
  • 1
    Is self.dictionary declared with let or var? – Encore PTL Nov 18 '14 at 16:09
  • 2
    Believed to be a compiler bug. I've experienced the same. See http://stackoverflow.com/questions/26809986/exc-bad-access-on-ios-8-1-with-dictionary – steventnorris Nov 18 '14 at 19:42
  • 1
    Thanks @juanelomx !!! Same bug and hack with `let` helped me – Aznix Dec 11 '14 at 15:29
  • Thanks for the hack, it really works! But I had to do some modifications - In my case it was not NSExpression that was triggering this but rather using Alamofire to trigger some web requests. What helped me was creating a `let` constant every time before I change a value in the dictionary. Hope this helps someone. – Gasper Feb 02 '15 at 09:45
  • Feels like @steventnorris should get the points for sending OP to the right answer. – bsarrazin Mar 13 '15 at 03:03
  • @bensarz I added an answer below to help those looking for answers. – steventnorris Mar 13 '15 at 14:42
  • A solid fix has now been found I believe. I've added the solution to my answer. It is a compiler optimization level bug. – steventnorris Mar 19 '15 at 16:23

1 Answers1

1

With @bensarz comment, I thought it might be helpful for others searching for answers if I put the response into an actual answer instead of a comment.

Per @LeeWhitney's response on a similar post:

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.

Solution: The issue seems to be solved by changing the 'Optimization Level' under the 'Swift Compiler - Code Generation' to 'None'. The issue seems to be with the 'Fastest' Compiler optimization level.

Also, a work around that I've found original before the compiler change:

If you use a let statement prior to assigning values in the dictionary, it seems to alleviate the issue. More information found at link below:

EXC_BAD_ACCESS on iOS 8.1 with Dictionary

Community
  • 1
  • 1
steventnorris
  • 5,656
  • 23
  • 93
  • 174