9

I am trying to generate graph and take its screenshot one by one. The application works fine for once and then when I try to capture screenshots again the second time, the application crashes saying Application terminated due to memory error. This happens only in iOS8 not in iOS7. Here the the code to capture the screenshot, it crashed on line - [view.layer renderInContext:context]; The application uses 124 MB at peak usage and there is no memory warning generated before crashing. Even if there is no other application running in background the application crashes on iOS 8. And if I comment out the above line the application do not crash at all, but screenshot taken is not of complete screen.

- (BOOL)captureView:(UIView *)view forGraph:(NSString *)graphName
{

BOOL isImageCpatured = NO;

@try {
    CGRect rect = view.bounds;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGImageRef imageRef;

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake(0, 113, 1024, 532));
    }
    else {
        imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake(0, 93, 1024, 532));
    }

    img = nil;

    UIImage *image = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);        
    NSData *pngData = UIImagePNGRepresentation(image);

    [pngData writeToFile:[Utility_Class documentsPathForFileName:[NSString stringWithFormat:@"%@BG.png",graphName]] atomically:YES];

    pngData = nil;

    image = nil;

    isImageCpatured = YES;

}
@catch (NSException *exception) {
    return isImageCpatured;
}
@finally {

}

return isImageCpatured;
}

What could be wrong? I am not able to find any solution. I found out that memory pressure is different from memory error. But no solution how to fix it. Even the captureView function is called inside an @autorelease pool.

Here is the Unknown Crash Log that is generated on iOS8. iOS7 it never crashes.

Incident Identifier: 24F29058-7D47-40B6-87B4-2183220DB55B
CrashReporter Key:   6079e634bf7aeebb0e4d9ea95336cb33b0fc49dd
Hardware Model:      iPad4,1
OS Version:          iPhone OS 8.0 (12A365)
Kernel Version:      Darwin Kernel Version 14.0.0: Tue Aug 19 15:09:47 PDT 2014; root:xnu-2783.1.72~8/RELEASE_ARM64_S5L8960X
Date:                2014-09-25 18:21:25 +0530
Time since snapshot: 88 ms
Free pages:                              2219
Active pages:                            96945
Inactive pages:                          48102
Speculative pages:                       314
Throttled pages:                         0
Purgeable pages:                         2
Wired pages:                             77304
File-backed pages:                       40758
Anonymous pages:                         104603
Compressions:                            1467794
Decompressions:                          158957
Compressor Size:                         24980
Uncompressed Pages in Compressor:        119797
Page Size:                               16384
Largest process: my application Name
max_
  • 24,076
  • 39
  • 122
  • 211
Sharon Nathaniel
  • 1,467
  • 20
  • 28
  • > could you please provide the crash log ?? – Arun Kumar Sep 25 '14 at 13:13
  • I also have an app that's crashing, seemingly randomnly, in iOS 8 but not iOS 7. V strange... – mm2001 Sep 26 '14 at 05:20
  • Same issue with my apps. And it happens on iPhone 5s and 6, but not 5! The crash logs say it's a memory issue. @mm2001 do you use ARC? – mota Sep 28 '14 at 22:19
  • @Mota, yes I use ARC. Though I added Crashlytics and am now getting a repeatable stack trace with this error: http://stackoverflow.com/questions/19560198/ios-app-error-cant-add-self-as-subview. – mm2001 Sep 29 '14 at 04:31
  • Have you tried profiling your app in Instruments Allocations? Preferably on both iOS 7 and iOS 8 and then comparing the results? Could be a bug in iOS 8. – Nitzan Wilnai Sep 30 '14 at 18:58
  • @NitzanWilnai Yes I tried that also, the memory usage remains equal on both iOS7 and iOS8. That't what surprised me; even being under threshold, how can the app crash giving memory error. – Sharon Nathaniel Oct 01 '14 at 04:02
  • is your issue similar to this one: http://stackoverflow.com/questions/10129106/drawrect-renderincontext-issues – Steve Rosenberg Oct 03 '14 at 16:05
  • @MinnesotaSteve no the issues is different. We have not override drawrect, and the crash only occurs in iOS8. The app works great in iOS7. – Sharon Nathaniel Oct 03 '14 at 16:50
  • There is no exception thrown, right? `UIKit` does not support exceptions so there should never be `@try`, `@catch` and `@finally` anywhere in the code. -- Also try using `UIGraphicsBeginImageContextWithOptions(rect.size, NO, view.layer.contentsScale)` so that the correct scaling is applied. -- Also try using `UIView`'s `drawViewHierarchyInRect:afterScreenUpdates:` instead of `CALayer`'s `renderInContext:`. – fluidsonic Oct 07 '14 at 20:04
  • @fluidsonic try. catch and finally fails to catch the exception as there is none and application crashes. I tried the other options but all works great in iOS7 but when the same code runs on iOS8 it simply crashes app with memory error not memory pressure. – Sharon Nathaniel Oct 08 '14 at 04:08
  • Can you post the rendering code? Maybe there is an issue. – fluidsonic Oct 08 '14 at 07:46
  • @fluidsonic we have not overridden renderInRect. Or something else you want ? I cannot understand rendering code, its an OpenGL chart that is rendered, and we take the screenshot of the background of the chart since OpenGL cannot be captured at the same time. – Sharon Nathaniel Oct 08 '14 at 09:11
  • Can you upload an example project where we can reproduce this problem? This would make finding the cause much easier in this case. – fluidsonic Oct 08 '14 at 09:32
  • ok I will try to create a example project and see if the problem can be reproduced and then share it. – Sharon Nathaniel Oct 08 '14 at 10:52

1 Answers1

0

This is due to a bug with CGImageCreateWithImageInRect. See this: http://openradar.appspot.com/radar?id=5780787475513344

Source: https://twitter.com/steipete/status/524224268952158208

Eric
  • 16,003
  • 15
  • 87
  • 139