I’m really confused here. I am doing some image processing in Cocoa Touch and everything works well. My App uses about 120MB of memory, which is fine, and there are no leaks at all, which I checked using Instruments. However when I run the routine a second time with a completely new instance of my image processing class (after deleting the first of course) the app crashes on my iPhone 4. It still works fine on my iPad 3. Whenever I call “CGContextDrawImage()” is crashes. Obviously there is no exception thrown or anything the like. I really don’t know why this is, since there should be enough memory available. Running it in Instruments on my iPad 3 shows that the never uses more than 120MB of memory.
Please find the code which causes the crash below. Again, it only crashes when doing this a second time and only on my iPhone 4.
CGImageRef imageref = [image1 CGImage];
uint8_t *inBitmap1 = (uint8_t *) malloc(vectorsizeI);
CGContextRef context1 = CGBitmapContextCreate(inBitmap1, width, height, bitsPerComponent, bytesPerRowI, colorSpace, kCGImageAlphaNoneSkipFirst);
CGContextDrawImage(context1, CGRectMake(xOffset1, yOffset1, imwidth, imheight), imageref);
There’s obvioulsy not enough memory. Maybe there is on iPad but there isn’t on the iPhone. Keep in mind that the OS reserves only a small part of the physically available RAM (0.5 or 1 GB) for your app, and if it exceeds that limit, the system terminates the seemingly harmful process. Try to reduce your app’s memory fingerprint somehow, 120 MB is not “fine”.