0
votes

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;
            }
        }

    }
1
Please provide the code where you are adding items to the dataforScope array. - Joel
@Joel, I edited my question. Thank you! - jcr
Your NSMutableArray addObject code is fine, so that's not the issue. How are you allocating and freeing tempBuffer.mData - Joel
Why is samples declared as a pointer to an integer, but then you are using it as an array of integers? Does samples[i] return an integer or pointer? - Joel
Are -refreshScope: and -processAudio: always called from the same thread and/or GCD queue? If not, you have a locking issue with the dataForScope array. - Eric Skroch

1 Answers

0
votes

Make sure [[audio dataForScope] removeAllObjects]; executes before you call -reloadData on the plot. This ensures the dataForScope array is ready whenever the plot requests its new data.

The plot datasource methods are called on the main thread. If they read the dataForScope array directly, you need to make sure all accesses to that array (reads and writes) occurs on the main thread so there are no conflicts.