exc bad access – objective c losing NSDecimalNumber value when accessed from another method – Education Career Blog

I am parsing Json Data into an NSDecimalNumber as follows

product.Price = NSDecimalNumber decimalNumberWithDecimal:jProduct
objectForKey:@"Price" decimalValue;

This is in a loop with each product being added to an array. At the end I release the json object.

If I Log the value of product.Price during the loop the value is correct. However later on the the application lifecycle I access a product in the array and bang, I get EXC_BAD_ACCESS.

is this some odd pointer issue back to the json data which is then being released? any ideas? It’s worth noting that the values of my NSIntegers and NSStrings are fine.


You didn’t show all the relevant code, but my guess is that the definition of the Price property (which should start with a lower case letter, BTW) does not have the retain keyword. The result is that the autoreleased object from NSDecimalNumber decimalNumberWithDecimal:... simply gets autoreleased 🙂 And then your pointer is pointing to invalid memory.

The property should be declared similar to this:

@property(retain) NSDecimal *Price;

If the retain keyword is missing then only the pointer will be stored, but the property will not increase the retain count (tell the object: I’ll need you to stay around until I’m done with you).

Leave a Comment