I'm drawing a waveform of the incoming audio in the microphone by using core plot. It works great but i sometime get this error : malloc: * error for object 0x175a1550: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug. It happens occasionally (after 1 minute, 10 minutes, 1 hour...it depends!) when the core plot array is being cleared.
-(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];
}
The dataForScope array (mutable) is alloc/init in the audio class of my code. It is filled with integer of the audio buffer. I have tried a lot of different things but nothing seems to work. I always get the same error Any ideas ? Thank you.
EDIT :
-(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;
}
}
}
-refreshScope:and-processAudio:always called from the same thread and/or GCD queue? If not, you have a locking issue with thedataForScopearray. - Eric Skroch