Search code examples
iosios6exc-bad-accesscore-foundationcfdata

EXC_BAD_ACCESS on iOS 6 but not 5 when changing bytes of a CFDataRef


I have an application that applies various filters to an image. It works great on iOS 5 but crashes on 6. Below is a sample of where it's crashing:

CGImageRef inImage = self.CGImage;
CFDataRef m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); 
UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef); 

int length = CFDataGetLength(m_DataRef);

for (int i=0; i<length; i+=4)
{
    if(filter == filterCurve){

    int r = i;
    int g = i+1;
    int b = i+2;

    int red = m_PixelBuf[r];
    int green = m_PixelBuf[g];
    int blue = m_PixelBuf[b];

    m_PixelBuf[r] = SAFECOLOR(red); //    <==== EXC_BAD_ACCESS (code = 2)
    m_PixelBuf[g] = SAFECOLOR(green);
    m_PixelBuf[b] = SAFECOLOR(blue);
   }
}

Notice the bad access point when I try to assign a value back to m_PixelBuf. Anybody have any idea why this is occuring? What in iOS 6 would cause this?


Solution

  • This solves the problem: http://www.iphonedevsdk.com/forum/iphone-sdk-development/108072-exc_bad_access-in-ios-6-but-not-in-ios-5.html

    In iOS 6 you need to use CFDataCreateMutableCopy() (instead of CGDataProviderCopyData()), followed by CFDataGetMutableBytePtr() (instead of CFDataGetBytePtr()) if you're going to be manipulating the data's bytes directly.