Am I reconstructing the buffers correctly I am writing a networked audio application that sends the audio in 320 byte chunks, later I need to provide the player a bigger amount of bytes to play so I try to merge 2 or more chunks into one buffer and return it, well the code works but I hear some small audio artifacts, any ideas if the code is correct?

-(NSData*) getRawBufferWithSize:(int) requestedBufferSize
{
    if(audio.isBuffering &amp;&amp; audio.bufferTimer&gt;[[NSDate date] timeIntervalSince1970] &amp;&amp; !AUDIO_BYPASS_NET)
    {
        return nil;
    }
    audio.isBuffering = NO;
    int readyBufferSize = 0;
    int bytesPending = requestedBufferSize;

    if(returnBufferSize&lt;requestedBufferSize)
    {
        returnBuffer = (char*)realloc(returnBuffer, requestedBufferSize);
        returnBufferSize = requestedBufferSize;
    }

    while (readyBufferSize&lt;requestedBufferSize)
    {
        NSData* newBuffer;
        @synchronized(playbackRawBuffer)
        {
            if([playbackRawBuffer count] != 0)
            {
                newBuffer = [[playbackRawBuffer objectAtIndex:0] retain];
                [playbackRawBuffer removeObjectAtIndex:0];
            }
            else
            {
                break;
            }
        }        
        char* bytes = (char*)[newBuffer bytes];

        int readableBytes = newBuffer.length;
        int amountOfBytesToRead = bytesPending;
        int amountOfBytesToSaveForLater = 0;

        if(readableBytes&lt;amountOfBytesToRead)//if we can read less than we want
        {
            amountOfBytesToRead = readableBytes;
        }
        else if(readableBytes&gt;amountOfBytesToRead)//if we can read more bytes than we want (we save the remaining bytes)
        {
            amountOfBytesToSaveForLater = readableBytes - amountOfBytesToRead;
        }

        bytesPending -= amountOfBytesToRead;
        memcpy((char*)returnBuffer+readyBufferSize, bytes, amountOfBytesToRead);
        [newBuffer release];

        readyBufferSize+=amountOfBytesToRead;

        if(amountOfBytesToSaveForLater&gt;0)
        {//save these bytes back in the buffers array
            if(tBufSize&lt;amountOfBytesToSaveForLater)
            {
                tBuf = (char*)realloc(tBuf, amountOfBytesToSaveForLater);
                tBufSize = amountOfBytesToSaveForLater;
            }
            memcpy(tBuf, &amp;(bytes[amountOfBytesToRead]), amountOfBytesToSaveForLater);
            @synchronized(playbackRawBuffer)
            {
                [playbackRawBuffer insertObject:[NSData dataWithBytes:tBuf length:amountOfBytesToSaveForLater] atIndex:0];
            }
            break;
        }
    }

    if(readyBufferSize&gt;0)
    {
        return [NSData dataWithBytes:returnBuffer length:readyBufferSize];
    }
    else
    {
        audio.isBuffering = YES;
        audio.bufferTimer = [[NSDate date] timeIntervalSince1970]+0.1;
        return nil;
    }
}
This is the wrong way to sue realloc:

returnBuffer = (char*)realloc(returnBuffer, requestedBufferSize);


If it fails you have just leaked the old returnBuffer.
The way realloc should be used is:

char* tmp = (char*)realloc(returnBuffer, requestedBufferSize);
if (tmp != NULL)
{
    returnBuffer = tmp;
}
else { /* Deal with realloc() failure */ }


But why are you dealing with this. I would use a std::vector&lt;char&gt; it deals with all the memory management issues for you (and your post id tagged C++).

TO be honest I have trouble following the rest of your code.