奇怪的EXC_BAD_ACCESS
我的 Objective-C 代码中有一个 for 循环,它不断抛出 EXC_BAD_ACCESS
错误。代码如下:
double (*X)[2];
for (int bufferCount=0; bufferCount < audioBufferList.mNumberBuffers; bufferCount++) {
SInt16* samples = (SInt16 *)audioBufferList.mBuffers[bufferCount].mData;
for (int i=0; i < 1024; i+=2){//numSamplesInBuffer / 2; i+=2) {
X[i][0] = samples[i];
X[i][1] = samples[i + 1];
NSLog(@"left: %f", X[i][0]);
NSLog(@"right: %f", X[i][1]);
NSLog(@"i: %d", i);
}
}
当i = 385
时,我在NSLog(@"left: %f: ", X[i][0]);
.
我认为这可能是本地声明 X 的内存问题,因此将 X 更改为导致 EXC_BAD_ACCESS
的属性> 第一次通过时 for 循环的第一行。
有人知道为什么会发生这种情况吗?
I have a for loop in my Objective-C code that keeps throwing a EXC_BAD_ACCESS
error. Here is the code:
double (*X)[2];
for (int bufferCount=0; bufferCount < audioBufferList.mNumberBuffers; bufferCount++) {
SInt16* samples = (SInt16 *)audioBufferList.mBuffers[bufferCount].mData;
for (int i=0; i < 1024; i+=2){//numSamplesInBuffer / 2; i+=2) {
X[i][0] = samples[i];
X[i][1] = samples[i + 1];
NSLog(@"left: %f", X[i][0]);
NSLog(@"right: %f", X[i][1]);
NSLog(@"i: %d", i);
}
}
When i = 385
, I get a EXC_BAD_ACCESS
at the line NSLog(@"left: %f:", X[i][0]);
.
Thinking it may be a memory issue with X being declared locally, I changed X to a property which caused a EXC_BAD_ACCESS
at the first line of the for loop on the first time through.
Anyone know why this may be happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 HotLicks 指出的那样,问题是我没有为数组分配任何空间。解决方案是像这样初始化数组:
As HotLicks pointed out, the problem was that I wasn't allocating any space for the array. The solution was to initialize the array like this: