BAD ACCESS EXC

Hi everyone,


I am trying to create a project that uses audio streaming from unity. For this, I am developing a plugin.


At the time of recording the audio has no problem and I send the data through a websocket in a base64 string.

Then from xcode I catch it and transform it to NSDATA, that's the problem. At first there are no problems but after a moment, Xcode show me the error EXC_BAD_ACCESS and I can't continue copying the NSDATA into the buffer.


Here is the code.


#import "AudioProcessor.h"
  
#pragma mark Playback callback

static OSStatus playbackCallback(void *inRefCon,
                                 AudioUnitRenderActionFlags *ioActionFlags,
                                 const AudioTimeStamp *inTimeStamp,
                                 UInt32 inBusNumber,
                                 UInt32 inNumberFrames,
                                 AudioBufferList *ioData) {


    AudioProcessor audioProcessor = (AudioProcessor) inRefCon;

   // copy buffer to audio buffer which gets played after function return
    if(ioData->mNumberBuffers > 0) {

         AudioBuffer buffer = ioData->mBuffers[0];

// get the data from Unity
         NSString *inputData = audioProcessor.getInputData;

        if(inputData && ![inputData isKindOfClass:[NSNull class]])
        {

//here it's the problem.
            NSData *data = [[NSData alloc] initWithBase64EncodedString:inputData options:0];

            memcpy(buffer.mData, data.bytes, data.length);
            buffer.mDataByteSize = (int) data.length;
        }

    return noErr;
}

#pragma mark controll stream

-(void)setInputData:(NSString *)datosValue
{
    inputData = datosValue;
}

-(NSString*)getInputData
{
    return inputData;
}


If someone knows how it could be done so that the application does not close, I would appreciate it.

Replies

are you sure buffer.mData is large enough to hold all data you need to copy ?

Yes.. I try with a lot of differents ways but always the EXC_BAD_ACCESS appear 😟

So now you have to approach the problem a bit more carefully:


1. "[[NSData alloc] initWithBase64EncodedString:inputData options:0]" is documented to return nil if the data cannot be decoded. You do not test the return value, but you should. If it returns nil, the memcpy will fail with EXC_BAD_ACCESS. Otherwise …


2. You need to prove that you are not causing a buffer overflow with the memcpy. What is the actual value of data.length when it fails? What is the actual length of the mData buffer, and how do you know that? Note that your code assumes that "mDataByteSize" represents the portion of the buffer containing valid data, not the total size of the buffer. Is that assumption correct? How are mData buffers created?


You will need to address all of that specifically, if you want to solve your problem.