27

Update: I am using XCode 7 Beta with Swift 2.0

When I try to run my app on an iPhone I get these 3 errors:

<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextTranslateCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

When I enable to CG_CONTEXT_SHOW_BACKTRACE variable I get this:

Backtrace:
      <<redacted>+40>
       <<redacted>+432>
        <<redacted>+144>
         <<redacted>+172>
          <<redacted>+332>
           <<redacted>+112>
            <<redacted>+820>
             <<redacted>+688>
              <<redacted>+356>
               <<redacted>+80>
                <<redacted>+808>
                 <<redacted>+344>
                  <<redacted>+420>
                   <<redacted>+104>
                    <<redacted>+284>
                     <<redacted>+556>
                      <<redacted>+504>
                       <<redacted>+1792>
                        <<redacted>+220>
                         <<redacted>+284>
                          <<redacted>+920>
                           <<redacted>+168>
                            <<redacted>+184>
                             <<redacted>+56>
                              <<redacted>+24>
                               <<redacted>+540>
                                <<redacted>+724>
                                 <CFRunLoopRunSpecific+384>
                                  <<redacted>+460>
                                   <UIApplicationMain+204>
                                    <main+164>

I'm not sure how to interpret this, I am not using any CoreGraphics content in my code, and the other similar questions had answers that did not work for me. I understand it can be difficult to help debug without code, but I can't figure out what code is related to this, so if anyone has an idea I can post more code.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
mattgabor
  • 2,064
  • 6
  • 25
  • 38
  • 1
    see [this post](http://stackoverflow.com/questions/19508048/cgcontext-invalid-context-0x0?rq=1) it might be helpful – Vladmrnv Aug 26 '15 at 20:39

7 Answers7

52

From my experimentation, this seems to be due to defining UIViewControllerBasedStatusBarAppearance in Info.plist.

It happens on iOS 9 (beta & GM) but not on 8.4.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Mark Krenek
  • 4,889
  • 3
  • 25
  • 17
  • Wow thanks, that was the cause...I will submit a ticket – mattgabor Sep 08 '15 at 18:05
  • 19
    This is still happening in the released version of Xcode 7.0. – picciano Sep 17 '15 at 16:10
  • 7
    Had this still in XCode 7 GM, iOS9 GM. But, check it out: Set `UIViewControllerBasedStatusBarAppearance` to **YES** (even though the status bar is hidden, which is why I set it to NO to begin with), and the warnings go away. Crazy! – Olie Sep 25 '15 at 04:18
  • 3
    @Olie : Your answer worked for me. You should make it an answer instead of a comment. – Jim Rota Sep 28 '15 at 20:12
  • [might help](http://stackoverflow.com/a/31887785/3577656) and here is apple [thread](https://forums.developer.apple.com/thread/13683) in which apple staff said,this warning can be avoided for now and it is a known issue. – Rajal Oct 05 '15 at 10:07
  • I have reworded this answer so that it doesn't look like a "me too!" comment. See [this meta post](http://meta.stackoverflow.com/q/307568/560648) for more information. – Lightness Races in Orbit Oct 08 '15 at 16:08
  • I'm having the same issue in Xcode 8.2 (I'm rendering an UIImageView that will actually never be visible, serves only as a holder to merge two images together which is probably the issue as it points to use of CIImage instead of UIImage then). – igraczech Jan 16 '17 at 15:35
  • Does not really have an effect in Xcode 8.2. Adding/removing this key makes no difference. – igraczech Jan 16 '17 at 15:35
11

At @JimRota's suggestion, moving this into an answer, since it was deemed helpful:

Had this still in XCode 7 GM, iOS9 GM. But, check it out: Set UIViewControllerBasedStatusBarAppearance to YES (even though the status bar is hidden, which is why I set it to NO to begin with), and the warnings go away. Crazy!

I then had to add this to my view controllers to keep the status bar hidden:

- (BOOL) prefersStatusBarHidden
{
    return YES;
}

For me, all my VCs actually inherit from the ProjectNameViewController, so that simplified it a bit.

Olie
  • 24,597
  • 18
  • 99
  • 131
2

im getting the same thing on the xcode 7 GM release, i think ill simply ignore them since everything works but its pretty annoying even though they have admitted its a bug and just didnt fix it yet. Dev Forum Posting

user3099837
  • 3,931
  • 5
  • 15
  • 13
2

from comments given above , this is the real way to fix :

UIViewControllerBasedStatusBarAppearance to YES in Info.plist
Logic
  • 705
  • 1
  • 9
  • 27
1

I am getting this too (including the redacted part) when compiling my project on iOS9 beta5. Its fine when compiling on iOS8.

It happens right in my App Delegate, before executing any commands from

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

so it seems more likely to be an iOS beta problem than ours.

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
Peter Johnson
  • 3,764
  • 1
  • 23
  • 27
  • 1
    I have added that I am using XCode 7 beta and swift 2, but I think this was occurring on XCode 7 as well, – mattgabor Sep 01 '15 at 17:41
0

I had the same type of bug when using non integer values as insets for resizable images, like this:

UIImage * image = [[UIImage imageNamed:@"pill"]
             resizableImageWithCapInsets:UIEdgeInsetsMake(17.5, 17.5, 17.5, 17.5)];

This caused this kind of logs. Using integer values fixed the problem.

Pang
  • 9,564
  • 146
  • 81
  • 122
madewulf
  • 1,870
  • 4
  • 26
  • 41
-1

I had this error. It was because I created a function and forgot to add a return value.

-(void)doSomething

vs.

-doSomething

ugh.

Lee
  • 306
  • 1
  • 7