1

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.

shallowThought
  • 19,212
  • 9
  • 65
  • 112
James
  • 459
  • 1
  • 3
  • 10
  • what is the price value? – Dharma Jun 13 '17 at 12:23
  • In getModelTypePrice, if I print it out before returning, I get the following: `prices[0].new_price: Optional("369737")` – James Jun 13 '17 at 12:29
  • Try this ... self.newPrice = prices.new_price! – Bala Jun 13 '17 at 12:32
  • @Bala Just tried, didn't make a difference. But both variables are optional Strings, so I shouldn't need to. I also tried `price.new_price == nil ? "0" : price.new_price!` but same result. – James Jun 13 '17 at 12:36
  • how you declare the variable newPrice? – Bala Jun 13 '17 at 12:43
  • @Bala like so: `var newPrice: String? = "0"`. I also tried making it non-optional and using `price.new_price == nil ? "0" : price.new_price! ` so set it, but that also made no difference. – James Jun 13 '17 at 12:54
  • Check prices.new_price is String or number or share your full code will fix it. – Bala Jun 13 '17 at 13:05

1 Answers1

1

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.

James
  • 459
  • 1
  • 3
  • 10