我正在使用核心图绘制麦克风中传入音频的波形。 它工作得很好但我有时会收到此错误: malloc: *对象0x175a1550的错误:未分配被释放的指针 * 在malloc_error_break中设置断点以进行调试。 当核心绘图数组被清除时偶尔发生(1分钟,10分钟,1小时......这取决于!)。
-(void)refreshScope: (NSTimer*)theTimer;
{
if ([[audio dataForScope] count] == 500)
{
[self performSelectorOnMainThread:@selector(reloadDataOnMainThread) withObject:nil waitUntilDone:YES];
[[audio dataForScope] removeAllObjects]; // HERE !!!!
}
}
-(void) reloadDataOnMainThread
{
[audioScope reloadData];
}
dataForScope数组(可变)是我的代码的音频类中的alloc / init。它充满了音频缓冲区的整数。 我尝试过很多不同的东西,但似乎没什么用。我总是得到同样的错误 有任何想法吗 ? 谢谢。
编辑:
-(void)processAudio:(AudioBufferList *)bufferList{
AudioBuffer sourceBuffer = bufferList->mBuffers[0];
memcpy(tempBuffer.mData, bufferList->mBuffers[0].mData, bufferList->mBuffers[0].mDataByteSize);
int16_t* samples = (int16_t*)(tempBuffer.mData);
@autoreleasepool
{
for ( int i = 0; i < tempBuffer.mDataByteSize / 2; ++i )
{
if(i % 5 == 0)
{
if ([dataForScope count] == 500)
{
scopeIndex = 0;
}
else
{
float scopeTime = scopeIndex * 1000.0 / SampleRate;
id xScope = [NSNumber numberWithFloat: scopeTime];
id yScope = [NSNumber numberWithInt: samples[i]/100];
[dataForScope addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:xScope, @"xScope", yScope, @"yScope", nil]];
}
scopeIndex = scopeIndex + 1;
}
}
}
答案 0 :(得分:0)
确保[[audio dataForScope] removeAllObjects];
执行,然后才能在地图上调用-reloadData
。这可确保每当绘图请求其新数据时dataForScope
数组就绪。
在主线程上调用绘图数据源方法。如果他们直接读取dataForScope
数组,您需要确保在主线程上发生对该数组的所有访问(读取和写入),因此没有冲突。