I've identified a bug while developing against the iPhone OS 3.0 SDK. Basically, if I create a CGImage from a bitmap image context, I get the following error when I release it:
malloc: *** error for object 0x1045000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Here's the relevant code:
CGSize size = CGSizeMake(100, 100);
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
size_t bitsPerComponent = 8;
size_t bytesPerPixel = 4;
size_t bytesPerRow = size.width * bytesPerPixel;
void *bitmapData = calloc(size.height, bytesPerRow);
CGContextRef ctxt = CGBitmapContextCreate(bitmapData, size.width, size.height, bitsPerComponent, bytesPerRow, cs, kCGImageAlphaPremultipliedLast);
// we could draw something here, but why complicate things?
CGImageRef image = CGBitmapContextCreateImage(ctxt);
CGContextRelease(ctxt);
free(bitmapData);
CGColorSpaceRelease(cs);
CGImageRelease(image); // This triggers the error message.
The above example is self-contained and it's pretty clear no retain/release rules are being violated. I've tested this code on the iPhone Simulator under 3.0, 3.1, and 3.1.2. The problem only occurs under 3.0; it seems to have been fixed in 3.1 and later. I have not confirmed the bug on the device.