CGBitmapContextCreate gives EXC_BAD_ACCESS in iPhone 6+

357 views Asked by At

When calling CGBitmapContextCreate method the app crashes giving EXC_BAD_ACCESS.

This happens only in iPhone 6+ (when running on on device).

CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);

And yes, I've tried setting NSZombieEnabled to YES as an environment variable.. but still nothing is getting displayed on the debugger console upon EXC_BAD_ACCESS.

1

There are 1 answers

0
Charlie Scott-Skinner On

I was having a crash with this exact function because I was allocating a storage buffer with no respect to its alignment, e.g:

baseAddress = malloc (width * height * 4);

Now I use...

baseAddress = new u32 [width * height];

...to force a 4 byte alignment and it fixed the EXC_BAD_ACCESS. Note, I also use kCGBitmapByteOrder32Big instead of little as you are. May or may not be relevant.

However, I get weird pixel data back on an iPhone 6 and an iPad Mini.

(See Getting pixel data on iOS 8 / iPhone 6).