James
James

Reputation: 492

Receiving EXC_BAD_ACCESS exception

I am using a singleton class to select data from CoreData, and send it back to the calling ViewController. My issue is that when getting one of the ManagedObject's properties, the app crashes with an EXC_BAD_ACCESS exception.

This only seems to happen on iOS 9.x or on the simulator, but is pretty consistent on those. It hasn't happened on a device running 10.x. I set the scheme diagnostics to show zombie objects, and am now presented with the following error:

-[CFString copy]: message sent to deallocated instance 0x15b92990

The issue is that the string being referenced is on an object retrieved directly before I get this error, and I am using Swift (So not manually deallocating anything), so I don't understand why it is deallocated.

The code that selects the object looks like this:

func getModelTypePrice(mmCode: String, year: Int) -> ModelTypePrice? {
    let request = NSFetchRequest<ModelTypePrice>(entityName: "ModelTypePrice")
    request.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [NSPredicate(format: "mmcode = %@", mmCode),
                                                                        NSPredicate(format: "reg_year = %d", year)])
    do {
        let prices = try managedContext.fetch(request)
        if prices.count == 1 {
            return prices[0]
        }
    } catch {
        print("Error selecting object: \(error)")
    }
    return nil
}

That is called from the ViewController, and used as follows:

if let price = LibraryAPI.sharedInstance.getModelTypePrice(mmCode: "123", year: 2017) {
    self.newPrice = price.new_price // Error happens here.
}

The ViewController does have an optional String property called newPrice. The new_price property on a ModelTypePrice is also an optional String. I am at a bit of a loss here, so any advice or suggestions would be appreciated.

Upvotes: 1

Views: 88

Answers (1)

James
James

Reputation: 492

This fixed it: [CFNumber release]: message sent to deallocated instance

The problem was the name of the property of the managed object starting with new(it was new_price). Changing it to price_new fixed it. Apparently they changed how this is handled in iOS 10.x, as it was never a problem there.

Maybe this saves someone else some frustration.

Upvotes: 1

Related Questions