由于EXC_BAD_ACCESS,renderInContext有时会崩溃

时间:2014-10-16 06:39:47

标签: ios

我尝试了以下步骤来捕获屏幕截图。

CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions) {
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
}
else
{
    UIGraphicsBeginImageContext(imageSize);
}
CGContextRef context = UIGraphicsGetCurrentContext();
@try {
    for (UIWindow * window in [[UIApplication sharedApplication] windows]) {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            CGContextConcatCTM(context, [window transform]);
            CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y);

            dispatch_sync(dispatch_get_main_queue(), ^{
                LogInfo(@"before renderInContext on main thread");
                [[window layer] renderInContext:context];
                LogInfo(@"after renderInContext  on main thread");
            });

            CGContextRestoreGState(context);
        }
    }
}
@catch (NSException *exception) {
    LogInfo(@"catch NSException %@ in captureScreen",[exception name]);
}
@finally {

}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但有时崩溃, 崩溃日志:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)    
Exception Subtype: KERN_INVALID_ADDRESS at 0x11bd70d8    
Triggered by Thread:  0    
Thread 0 Crashed:    
0   CoreGraphics                  0x2f9829ce argb32_image_mark_argb32 + 402
1   CoreGraphics                  0x2f9827d2 argb32_image_mark_image + 706
2   CoreGraphics                  0x2f97e126 argb32_image + 2106
3   libRIP.A.dylib                0x2fccbafa ripl_Mark + 14
4   libRIP.A.dylib                0x2fcd61ac RIPLayerBltImage + 728
5   libRIP.A.dylib                0x2fcc9862 ripc_RenderImage + 434
6   libRIP.A.dylib                0x2fcc816e ripc_DrawImage + 662
7   CoreGraphics                  0x2f97b6f8 CGContextDelegateDrawImage + 48
8   CoreGraphics                  0x2f97b57c CGContextDrawImage + 280
9   QuartzCore                    0x31dfa642 -[CALayer _renderForegroundInContext:] + 2266
10  QuartzCore                    0x31df976a -[CALayer renderInContext:] + 1222
11  QuartzCore                    0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
12  QuartzCore                    0x31df9782 -[CALayer renderInContext:] + 1246
13  QuartzCore                    0x31df98b4 -[CALayer renderInContext:] + 1552
14  QuartzCore                    0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
15  QuartzCore                    0x31df9782 -[CALayer renderInContext:] + 1246
16  UIKit                         0x32721322 -[_UIBackdropViewLayer renderInContext:] + 78
17  QuartzCore                    0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
18  QuartzCore                    0x31df9782 -[CALayer renderInContext:] + 1246
19  QuartzCore                    0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
20  QuartzCore                    0x31df9782 -[CALayer renderInContext:] + 1246
21  QuartzCore                    0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
22  QuartzCore                    0x31df9782 -[CALayer renderInContext:] + 1246
23  QuartzCore                    0x31dfaa08 -[CALayer _renderSublayersInContext:] + 292
24  QuartzCore                    0x31df9782 -[CALayer renderInContext:] + 1246
25  RuiKlasse                     0x00113598 0x8e000 + 546200
26  libdispatch.dylib             0x3a8cbb36 _dispatch_barrier_sync_f_slow_invoke + 66
27  libdispatch.dylib             0x3a8c5d3c _dispatch_client_callout + 20
28  libdispatch.dylib             0x3a8c86be _dispatch_main_queue_callback_4CF + 274
29  CoreFoundation                0x2f8cc674 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 4
30  CoreFoundation                0x2f8caf40 __CFRunLoopRun + 1304
31  CoreFoundation                0x2f8357a4 CFRunLoopRunSpecific + 520
32  CoreFoundation                0x2f835586 CFRunLoopRunInMode + 102
33  GraphicsServices              0x3476f6ce GSEventRunModal + 134
34  UIKit                         0x3219488c UIApplicationMain + 1132
35  RuiKlasse                     0x0024d46c 0x8e000 + 1832044
36  libdyld.dylib                 0x3a8daab4 start + 0    

这个问题每次都不会发生,但它恰好足以让我的应用无法使用(崩溃)。我的机智已经结束了。有谁知道如何解决它或给我一些提示来调查这个问题?

进一步的调查结果:

1.尽管我在主线程上运行了整个代码,但这个问题仍然存在。 2.KERN_INVALID_ADDRESS并未指向 context 。 (我得到 context 的地址,发现它们不同)

我做错了吗?你是怎么解决的?

0 个答案:

没有答案