异常 - 数组检查的界限是不对的?

时间:2012-07-23 20:00:42

标签: objective-c arrays exception bounds

关于为什么下面指出的行会抛出异常的任何线索?

-(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处得到对象...但是其他时候,它会报告低至{{1}的边界}}?

在视图控制器中(项目中只有VC一个)

theBufferManager = [[HitBufferManager alloc] init];

.h文件中的

: @property NSMutableOrderedSet * theTopValues;

.m文件中的

: @synthesize theTopValues;

- HitBufferManager的(id)init: theTopValues = [NSMutableOrderedSet orderedSetWithCapacity:numToStore]; //返回自动释放的版本。

添加/编辑各个项目:

[0 .. 2]

2 个答案:

答案 0 :(得分:0)

尝试更改:在.h文件中:@property NSMutableOrderedSet * theTopValues;

到.h文件:@property(强)NSMutableOrderedSet * theTopValues;

看看是否能解决问题。

答案 1 :(得分:0)

这确实是一个多线程问题。我正在排序和修改NSOrderedSet的代码位置周围的@synchronized {}很好地清理了我正在阅读它的部分中的问题。我现在的问题是试图找出我的其他线程来自哪里。是吗

CADisplayLink* gameTimer;
gameTimer = [CADisplayLink displayLinkWithTarget:self
                                        selector:@selector(updateDisplay:)];

[gameTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

和/或这是否会启动一个帖子?

 AudioUnitSetProperty(_effectState.rioUnit,
                      kAudioUnitProperty_SetRenderCallback,
                      kAudioUnitScope_Global, 
                      bus0, 
                      &callbackStruct, 
                      sizeof(callbackStruct);
 AudioOutputUnitStart(_effectState.rioUnit);