Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGContextSaveGState: invalid context 0x0 Error only on device

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.

like image 448
mattgabor Avatar asked Aug 26 '15 20:08

mattgabor


4 Answers

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.

like image 103
Mark Krenek Avatar answered Oct 20 '22 11:10

Mark Krenek


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.

like image 42
Olie Avatar answered Oct 20 '22 10:10

Olie


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

like image 2
user3099837 Avatar answered Oct 20 '22 10:10

user3099837


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

UIViewControllerBasedStatusBarAppearance to YES in Info.plist
like image 2
Logic Avatar answered Oct 20 '22 10:10

Logic