V3.1, Ch 7: Unit Testing - EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

I’ve been following along, typing the code from Core Data v3.1, Ch 7 Unit Testing.
After typing the code for “func testRootContextIsSavedAfterAddingCampsite()”, (on page 187-188), with no compiler errors or warnings, I run the test and get "Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

macOS Sierra v10.12.3
Xcode 8.2.1
iOS 10

Any thoughts?
Thank you.

What does your TestCoreDataStack class and your addCampiSite(…) method look like?

Thanks for your reply, tetanuss.

I just used FileMerge to compare my TestCoreDataStack and addCampSite() to that of the v3, ch 07-unit-testing final project and they’re identical, however I’ve run the tests in the final project several times and not seen the same problem.
I did see one other forum post that sounded like it is probably the same issue.



import CampgroundManager
import Foundation
import CoreData

class TestCoreDataStack: CoreDataStack {

  convenience init() {
    self.init(modelName: "CampgroundManager")

  override init(modelName: String) {
    super.init(modelName: modelName)

    let persistentStoreDescription = NSPersistentStoreDescription()
    persistentStoreDescription.type = NSInMemoryStoreType

    let container = NSPersistentContainer(name: modelName)
    container.persistentStoreDescriptions = [persistentStoreDescription]

    container.loadPersistentStores { (storeDescription, error) in
      if let error = error as? NSError {
        fatalError("Unresolved error \(error), \(error.userInfo)")

    self.storeContainer = container

  public func addCampSite(_ siteNumber: NSNumber, electricity: Bool, water: Bool) -> CampSite {
    let campSite = CampSite(context: managedObjectContext)
    campSite.siteNumber = siteNumber
    campSite.electricity = NSNumber(value: electricity)
    campSite.water = NSNumber(value: water)


    return campSite

Looks also good… I know it’s a stupid question :wink: . have you tried to clean the project?

1 Like

funny enough, cleaning the project seemed to resolve a similar issue for me.