计数用尽当我运行从SenTest的STAssertThrowsSpecificNamed
打了一个电话里这样的代码:保留后@throw
@throw [[NSException alloc] initWithName:NSInvalidArchiveOperationException
reason:@"---some reason----"
userInfo:nil];
我得到(与NSZombieEnabled=YES
):
*** -[NSException reason]: message sent to deallocated instance 0x100a81d60
唯一的例外是某种STAssertThrowsSpecificNamed
之前释放已完成处理。
我可以通过更换上面这段代码的@throw
行避免该错误:
NSException *exception = [NSException exceptionWithName:NSInvalidArchiveOperationException
reason:@"---some reason----"
userInfo:nil];
@throw exception;
我得到完全有或没有ARC相同的行为。没有弧这个代码也避免了错误:
@throw [[[NSException alloc] initWithName:NSInvalidArchiveOperationException
reason:@"---some reason----"
userInfo:nil] retain];
这是一个错误在SenTest?或编译器中的错误?或者是我的第一个@throw
只是不正确?
你想解释为什么第二个例子有效吗? – JeremyP 2012-03-06 12:11:56
并且是你在ARC下推荐的第二个例子,我不能使用'retain'? – paulmelnikow 2012-03-06 16:32:10
这样做的原因是因为我看到@throw释放了您创建的NSExceptions,因此如果您将-retain/-autorelease NSException传递给它,它将保留并在必要时释放。当“将对象赋予另一个对象”时,保留计数应该以这种方式使用,并且使用-autorelease,它只是意味着“必要时释放,或者将其留给垃圾回收”。 – 2012-03-06 17:16:11