0

This is a question related to this one.

When remove object from a mutable array, I noticed that there might be a 'retain' message sent to that object, so I searched out the above question, where w.m gave an answer mentioned that the internal implementation of NSMutableArray might first retain object, then release it twice when remove that object.

My question is that: Is there any evidence for this? Or anybody know any related details?

I met this issue when I was analyzing the following logs, I know there is something wrong with my code, but my concern is not about the bug itself, but whether it is a fact that 'there would be some retain work when removeObject'.

Exception Type:  EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes: 0x0000000000000001, 0x0000000000000000
Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Application Specific Information:
objc[299]: FREED(id): message retain sent to freed object=0x23f62a0

Thread 0 Crashed:  Dispatch queue: com.apple.main-thread

0   libobjc.A.dylib                 0x9a3694fd _objc_error + 116
1   libobjc.A.dylib                 0x9a369533 __objc_error + 52
2   libobjc.A.dylib                 0x9a36783a _freedHandler + 58
3   com.apple.CoreFoundation        0x9879a8cb -[NSMutableArray removeObject:range:identical:] + 331
4   com.apple.CoreFoundation        0x9879a770 -[NSMutableArray removeObject:] + 96
Community
  • 1
  • 1
sehone
  • 1
  • 2

1 Answers1

0

It doesn't matter what NSMutableArray does internally. It is of no concern to you. As long as it follows the memory management rules, i.e. it retains anything it needs to keep for later, and releases only things it has retained, it doesn't matter if it also retains and releases things 20 extra times in random places. Adding an extra retain-release pair never reduces the correctness of a program.

If you are getting a crash, then you are doing something wrong in your code.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • Thank you for your answer, newacct. I'm totally agree with you that there is something srong in my code (I've already found it out). – sehone Apr 15 '13 at 02:39
  • However, my concern is not about the bug itself, but the log info `message retain sent to freed object`. I've read docs from Apple, but never been told NSMutableArray would do some `retain` work during `removeObject`. So actually I'm expecting someone could share some evidence here. – sehone Apr 15 '13 at 02:53