iOS NSMutablearray发布行为

时间:2013-03-16 14:34:59

标签: ios objective-c memory-management nsmutablearray

我遇到NSMutableArray alloc和release的问题。见下面的代码:

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 错误中添加 backtrace 错误:

* 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

请向我解释一下,我做错了什么?

2 个答案:

答案 0 :(得分:4)

你根本不应该释放enemiesToDeleteArray。它将在当前范围结束时自动释放。您根本没有使用alloc,因此该对象是自动管理的。

答案 1 :(得分:2)

NSMutableArray *enemiesToDeleteArray = [NSMutableArray arrayWithCapacity:5];返回一个自动释放的实例,并在离开初始化程序后一段时间清理。现在指针enemiesToDeleteArray指向未定义的内存。你需要这样做:

enemiesToDeleteArray = [[NSMutableArray arrayWithCapacity:5] retain];

enemiesToDeleteArray = [[NSMutableArray alloc] initWithCapacity:5];