2

TLDR: I'm getting an EXC_BAD_ACCESS error using swift in XCode 6.1 building for iOS8.1. I believe the issue is likely a compiler error.

I am making an app wherein the user is allowed to add (Korean) words from a dictionary (in the English sense) to a word list. I have the following classes that define a word (with associated definitions, audio files, user-interaction statistics, etc.) and a word list (with an associated list of words and methods to add/remove words to the list, alphabetize the list, etc):

class Word: NSObject, NSCoding, Printable {
    var hangul = String("")
    var definition = String("")
    // ...
}

class WordList: NSObject, NSCoding {
    var title = ""
    var knownWords = Dictionary<String,String> ()
    // ...

    func addWordToList(wordName: String, wordDefinition: String) {
        // Debug statements
        println("I am WordList \"\(self.title)\" and have the following knownWords (\(self.knownWords.count) words total): ")
        for (_wordName,_wordDefinition) in self.knownWords {
            println("\t\"\(_wordName)\" : \(_wordDefinition)")
        }
        println("\nI am about to attempt to add the word \"\(wordName)\" with definition \"\(wordDefinition)\" to the above dictionary")

        // Add word to wordList, including the 'let' fix 
        fix_EXC_BAD_ACCESS_bug()
        knownWords[wordName] = wordDefinition // EXC_BAD_ACCESS
    }
    func fix_EXC_BAD_ACCESS_bug() {
        // This empty line attempts to solve a exc_bad_access compiler bug when adding a new value to a WordList dictionary
        let newDic = self.knownWords
    }
    // ...
}

Next I have a UITableViewController with a UISearchBar that I use to display the dictionary (again in the English sense) of words to the user. The user adds words by tapping a button (which is displaying an image) on the right of each cell, which calls the @IBAction func addWord() in the viewController:

class AddingWordsToWordList_TableViewController: UITableViewController, UISearchResultsUpdating {
    var koreanDictionary = KoreanDictionary() // Custom class with method 'getWord(index: Int)' to return desired Word
    var filteredSearch = [Word]()        
    // ...

    // Add word
    @IBAction func addWord(sender: UIButton) {
        // Add word
        if self.resultSearchController.active {
            let word = filteredSearch[sender.tag]
            wordList.addWordToList(word.hangul, definition: word.definition) // Enter addWordToList() here

        } else {
            let word = koreanDictionary.getWord(sender.tag)
            wordList.addWordToList(word.hangul, definition: word.definition)
        }

        // Set image to gray
        sender.imageView?.image = UIImage(named: "add133 gray")

        // Save results
        wordList.save()

        // Reload table data
        tableView.reloadData()
    }
    // ...

At first the app compiles and seems to run fine. I can add a new word list and start adding words to it, until I add the 8th word. I (always on the 8th word) get a EXC_BAD_ACCESS error in (WordList) addWordToList with the following println information:

I am WordList "School" and have the following knownWords (7 words total): 
"방학" : school vacation
"대학원" : graduate school
"학년" : school year
"고등학생" : high school student
"초등학교" : elementary school
"학생" : student
"학교" : school

I am about to attempt to add the word "대학생" with definition "college student" to the above dictionary

Note that the order in which I add the words appears irrelevant (i.e. the word "college student" can be added successfully if it is one of the first 7 words). There is nowhere in the code where I explicitly change behavior based on the number of words in a word list (except to display the words in a word list as cells in a UITableView), or any dependencies that would (to my knowledge) make the 8th word a special number. And indeed this number can change by using the let hack= solution (see below) to a different number, but for a particular build is always the same number.

At this point I'm at a complete loss of what to do. I'm relatively new to swift and have spent quite a few hours looking up how to fix exc_bad_access errors. I've come to the following point:

It seems exc_bad_access errors usually mean one of three things (http://www.touch-code-magazine.com/how-to-debug-exc_bad_access/)

  1. An object is not initialized
  2. An object is already released
  3. Something else that is not very likely to happen

I don't feel like either 1 or 2 can be the case for me, as right before we get the access error we can print out the contents of the dictionary in question (knownWords). However, as is always the case it is highly likely I'm missing something obvious, so please let me know if this is indeed true.

If the error is caused by 3 above ("something else") I have tried the following:

(From EXC_BAD_ACCESS on iOS 8.1 with Dictionary and EXC_BAD_ACCESS when updating Swift dictionary after using it for evaluate NSExpression solutions)

  1. I have tried different variations of the let stupidHack = scenario, and it does sometimes change the results, but only the number of entries allowed before crashing (i.e. I can sometimes get to the ~15th/16th entry in the dictionary before the error). I have not been able to find an actual, bug-free solution using this method, however.
  2. I have rebuilt using both Release and Debug modes; the error appears in both. Note I am only using the simulator and do not have a developer's license to try it on an actual device.
  3. I have turned off (set to "None") compiler optimization (it was already off for the Debug build anyway).
  4. I have tried building to iOS 8.0 with the same error results.

Any way to get around this issue is appreciated. A let hack = solution is acceptable, but preferably a solution will at least guarantee to not produce the error in the future under different build conditions.

Thanks!

Community
  • 1
  • 1
Cypress
  • 21
  • 1
  • It looks like you are trying to associate your `knownWords[wordName]` dictionary as a `string`, which won't work. `knownWords[wordName] = wordDefinition` - wordDefinition is a string, so if you want to add that to the dictionary you'll have to go about it differently. – l'L'l Mar 25 '15 at 17:54
  • Sorry I'm not quite sure what you mean. Apple's documentation (https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html) lists the following example: var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"] and then modifies the dictionary using airports["LHR"] = "London Heathrow". I'm trying to use the dictionary knownWords in the same way, where the word (i.e. 'hangul') is matched to its definition. – Cypress Mar 25 '15 at 18:12
  • Right, so when you use `[wordName]` (as a variable) is the value actually a key that already exists? – l'L'l Mar 25 '15 at 18:17
  • Not yet, wordName is a String passed into addWordToList as the key to the key-value pair (wordName, wordDefinition) I am attempting to add to the dictionary knownWords. Note that the code does run and appears to work fine the first 7 times the code is run through, but crashes on the 8th time through. – Cypress Mar 25 '15 at 18:22
  • There are far better tutorials on properly debugging your app; the only thing that is worth paying attention to on that link is enabling zombies, and setting breakpoints, but a good starting point perhaps. This is much better probably and mentions some key things that should help : http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1 – l'L'l Mar 25 '15 at 18:34
  • One more suggestion: your code is not that complex yet, so converting it to `objective-c` to see if you encounter similar behavior might save you time and possibly answer your question of whether it's your code or xcode. cheers!~ – l'L'l Mar 25 '15 at 18:46

0 Answers0