NSNumber> = 13 will not be saved. All the rest

The code I'm working on now requires adding an NSNumber object to the array. All NSNumbers with a value of 0-12 are added fine, but 13 of them call EXC_BAD_ACCESS. I have enabled NSZombieEnabled and am now getting *** -[CFNumber retain]: message sent to deallocated instance 0x3c78420 .

Here's the call stack:
#0 0x01eac3a7 in ___forwarding___
#1 0x01e886c2 in __forwarding_prep_0___
#2 0x01e3f988 in CFRetain
#3 0x01e4b586 in _CFArrayReplaceValues
#4 0x0002a2f9 in -[NSCFArray insertObject:atIndex:]
#5 0x0002a274 in -[NSCFArray addObject:]
#6 0x00010a3b in -[Faves addObject:] at Faves.m:24
#7 0x000062ff in -[ShowController processFave] at ShowController.m:458
#8 0x002af405 in -[UIApplication sendAction:to:from:forEvent:]
#9 0x00312b4e in -[UIControl sendAction:to:forEvent:]
#10 0x00314d6f in -[UIControl(Internal) _sendActionsForEvents:withEvent:]
#11 0x00313abb in -[UIControl touchesEnded:withEvent:]
#12 0x002c8ddf in -[UIWindow _sendTouchesForEvent:]
#13 0x002b27c8 in -[UIApplication sendEvent:]
#14 0x002b9061 in _UIApplicationHandleEvent
#15 0x02566d59 in PurpleEventCallback
#16 0x01e83b80 in CFRunLoopRunSpecific
#17 0x01e82c48 in CFRunLoopRunInMode
#18 0x02565615 in GSEventRunModal
#19 0x025656da in GSEventRun
#20 0x002b9faf in UIApplicationMain
#21 0x00002498 in main at main.m:14

If he had not been isolated from a certain range of NSNumbers, I would have suggested that I screwed something up with my memory management, but I just had no idea.

Any ideas?

Thanks,
Josh

+4
source share
2 answers

Numbers 0 through 12 are special, as I discovered when answering another question here . Keep in mind that this is an implementation detail, not a language specification.

In principle, the numbers before (including) 12 give you a link to an existing NSNumber, which is possible because they are immutable. The study showed that numbers 13 or more gave a separate copy.

So, you probably still ruined your memory management :-) Just because the numbers are less than 13, most likely are links to existing numbers that save your bacon in these cases. I suggest you post more code so that we can track this specific problem.


And based on your comment on another answer here:

I added a save line to the code, and now everything works fine. I do not know why. I'm just going to give it up. Thanks!

I think you will find that the fact that NSNumbers is less than 13 already has a save score of 1 before you get your own (by typing the score to 2), why they don't call EXC_BAD_ACCESS. Obviously, your code loses all the numbers that you allocate, but the system does not release those who are under the age of 13 because they are still in use (save the number of 1 or more).

+11
source

It is clear that NSNumbers> 12 will persist. I suggest you write a very small program that proves this to itself. Then take this program, make it a function, and call it at an early stage of your program. Slowly move the function to later points in your program until an error appears. Thus, you will find your real mistake.

+2
source

Source: https://habr.com/ru/post/1305359/


All Articles