我有NSMutableArray
alloc和釋放問題。見下面的代碼:的iOS NSMutable陣列釋放行爲
NSMutableArray *enemiesToDeleteArray = [NSMutableArray arrayWithCapacity:5];
...
else if ([enemy isActive] && !CGRectIntersectsRect(fieldOfView, enemyRect))
{
[enemiesToDeleteArray addObject:enemy];
}
...
[enemiesToDeleteArray removeAllObjects];
[enemiesToDeleteArray release];
上面的代碼在運行中崩潰。使用符號調試中斷錯誤「objc_release」或「malloc_break_error」。下面我加回溯從的發生objc_release錯誤:
* thread #1: tid = 0x1c03, 0x01e140a5 libobjc.A.dylib`objc_release + 21, stop reason = EXC_BAD_ACCESS (code=1, address=0xa0020010)
frame #0: 0x01e140a5 libobjc.A.dylib`objc_release + 21
frame #1: 0x01e14bd9 libobjc.A.dylib`(anonymous namespace)::AutoreleasePoolPage::pop(void*) + 555
frame #2: 0x023c6468 CoreFoundation`_CFAutoreleasePoolPop + 24
frame #3: 0x002effc1 QuartzCore`CA::AutoreleasePool::~AutoreleasePool() + 19
frame #4: 0x002ff35f QuartzCore`CA::Display::DisplayLink::dispatch(unsigned long long, unsigned long long) + 251
frame #5: 0x002ff75f QuartzCore`CA::Display::TimerDisplayLink::callback(__CFRunLoopTimer*, void*) + 161
frame #6: 0x023e3376 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
frame #7: 0x023e2e06 CoreFoundation`__CFRunLoopDoTimer + 534
frame #8: 0x023caa82 CoreFoundation`__CFRunLoopRun + 1810
frame #9: 0x023c9f44 CoreFoundation`CFRunLoopRunSpecific + 276
frame #10: 0x023c9e1b CoreFoundation`CFRunLoopRunInMode + 123
frame #11: 0x0312b7e3 GraphicsServices`GSEventRunModal + 88
frame #12: 0x0312b668 GraphicsServices`GSEventRun + 104
frame #13: 0x009d0ffc UIKit`UIApplicationMain + 1211
frame #14: 0x000c81f6 MyAPP`main(argc=1, argv=0xbffff36c) + 134 at main.m:14
frame #15: 0x00001fa5 MyAPP`start + 53
和malloc_break_error回溯:
MyAPP(18609,0xac98ea28) malloc: *** error for object 0x112dee40: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
(lldb) bt
* thread #1: tid = 0x1c03, 0x97be7815 libsystem_c.dylib`malloc_error_break, stop reason = breakpoint 1.1
frame #0: 0x97be7815 libsystem_c.dylib`malloc_error_break
frame #1: 0x97be8d51 libsystem_c.dylib`free + 346
frame #2: 0x023c5ea4 CoreFoundation`-[__NSArrayM dealloc] + 276
frame #3: 0x01e159ff libobjc.A.dylib`-[NSObject release] + 47
frame #4: 0x01e140d5 libobjc.A.dylib`objc_release + 69
frame #5: 0x01e14bd9 libobjc.A.dylib`(anonymous namespace)::AutoreleasePoolPage::pop(void*) + 555
frame #6: 0x023c6468 CoreFoundation`_CFAutoreleasePoolPop + 24
frame #7: 0x002effc1 QuartzCore`CA::AutoreleasePool::~AutoreleasePool() + 19
frame #8: 0x002ff35f QuartzCore`CA::Display::DisplayLink::dispatch(unsigned long long, unsigned long long) + 251
frame #9: 0x002ff75f QuartzCore`CA::Display::TimerDisplayLink::callback(__CFRunLoopTimer*, void*) + 161
frame #10: 0x023e3376 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
frame #11: 0x023e2e06 CoreFoundation`__CFRunLoopDoTimer + 534
frame #12: 0x023caa82 CoreFoundation`__CFRunLoopRun + 1810
frame #13: 0x023c9f44 CoreFoundation`CFRunLoopRunSpecific + 276
frame #14: 0x023c9e1b CoreFoundation`CFRunLoopRunInMode + 123
frame #15: 0x0312b7e3 GraphicsServices`GSEventRunModal + 88
frame #16: 0x0312b668 GraphicsServices`GSEventRun + 104
frame #17: 0x009d0ffc UIKit`UIApplicationMain + 1211
frame #18: 0x000c81f6 MyAPP`main(argc=1, argv=0xbffff36c) + 134 at main.m:14
frame #19: 0x00001fa5 MyAPP`start + 53
請解釋我這樣的人,我究竟做錯了什麼?
正如其他人所指出的那樣,簡單地刪除調用'release'。你也可以刪除對'removeAllObjects'的調用 - 在釋放期間''NSMutableArray'實例會自動處理它。 – jlehr 2013-03-16 15:31:27