关于为什么下面指出的行将引发异常的任何线索?例外 - 数组检查的边界是不对的?
-(double)calibrationValueAtIndex:(int)index
{
NSLog(@"count: %d index: %d",[theTopValues count], index);
return [[theTopValues objectAtIndex:index] doubleValue]; // exception happening here
}
2012-07-23 21:51:16.448 TestAppTimerAndHits[15130:f803] count: 9 index: 7
2012-07-23 21:51:22.339 TestAppTimerAndHits[15130:f803] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSOrderedSetM objectAtIndex:]: index 7 beyond bounds [0 .. 4]'
怎么能在该行的NSLog 9计数之前,则在异常报告说,边界是[0 .. 4]
?
异常似乎是随机抛出的..在某些情况下,数组边界是好的,这意味着我可以在索引8处获得对象...但是有些时候,它会报告范围低至[0 .. 2]
?
鉴于控制器(仅VC一个项目中的)
theBufferManager = [[HitBufferManager的alloc] INIT];
在.h文件中: @property NSMutableOrderedSet * theTopValues;
在.m文件: @synthesize theTopValues;对于HitBufferManager的(id)初始化: theTopValues = [NSMutableOrderedSet orderedSetWithCapacity:numToStore]; //返回一个自动发布的版本。
中的各个项目添加/编辑:
[self sortTopValues];
if([theTopValues count]<numToStore)
{
[theTopValues addObject:[NSNumber numberWithDouble:windowVal]];
}
else if(logVal> [[theTopValues objectAtIndex:0] doubleValue])
{
[theTopValues replaceObjectAtIndex:0 withObject:[NSNumber numberWithDouble:logVal]];
}
这不是一个多线程程序的片段,是吗? – dasblinkenlight 2012-07-23 20:09:58
我想知道...并想知道我是否在内存管理方面做了一些错误。我猜测出于某种原因,数组中的元素正在被垃圾收集?它是一个iOS/iPhone应用程序,我没有做任何事情来使它成为多线程,但它是我的第一个应用程序,所以我可能不知道它试图多线程的东西吗? – DoYouLikeHam 2012-07-23 20:21:45
如果是iOS版本,则不存在GC,只有裁判计数。不过,你可能有一个悬而未决的参考。你用ARC吗? – dasblinkenlight 2012-07-23 20:32:41