How can I set CG_CONTEXT_SHOW_BACKTRACE environmental variable?

69,649

Solution 1

It's a bug in 9b5:
https://forums.developer.apple.com/thread/13683

File a RADAR with Apple.

Solution 2

You can set an environmental variable via this menu: Product -> Scheme -> Edit Scheme. Under the "Run" item, locate the "Arguments" tab, you'll see "Environmental Variables" in the tab, Add CG_CONTEXT_SHOW_BACKTRACE and set its value to YES.

Solution 3

This problem happen with Xcode 7. In my case i removed the UIViewControllerBasedStatusBarAppearance from Info.plist and the problem solved! See the answer in this post:

Share:
69,649
Admin
Author by

Admin

Updated on February 15, 2020

Comments

  • Admin
    Admin about 4 years

    I have three buttons in my view. After setting the cornerRadus in the viewDidLoad() : button.layer.cornerRadius = 20 I get the following error message in the log:

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

    Questions:

    • How can I set CG_CONTEXT_SHOW_BACKTRACE environmental variable?
    • Or how can I fix this warning?

    Okay I've the backtrace now from the message. It doesn't make any sense to me. Some help please...

    Aug  7 14:27:00  <Error>: CGContextSaveGState: invalid context 0x0. Backtrace:
          <-[UIStatusBarItemView updateContentsAndWidth]+33>
           <-[UIStatusBarItemView initWithItem:data:actions:style:]+477>
            <+[UIStatusBarItemView createViewForItem:withData:actions:foregroundStyle:]+134>
             <-[UIStatusBarLayoutManager _createViewForItem:withData:actions:]+163>
              <-[UIStatusBarLayoutManager _prepareEnabledItemType:withEnabledItems:withData:actions:itemAppearing:itemDisappearing:]+36
               <-[UIStatusBarLayoutManager prepareEnabledItems:withData:actions:]+92>
                <-[UIStatusBarForegroundView _setStatusBarData:actions:animated:]+797>
                 <-[UIStatusBarForegroundView setStatusBarData:actions:animated:]+332>
                  <__51-[UIStatusBar _prepareToSetStyle:animation:forced:]_block_invoke+360>
                   <+[UIView(Animation) performWithoutAnimation:]+65>
                    <-[UIStatusBar _prepareToSetStyle:animation:forced:]+866>
                     <-[UIStatusBar _requestStyleAttributes:animationParameters:forced:]+391>
                      <-[UIStatusBar requestStyle:animationParameters:forced:]+437>
                       <-[UIStatusBar requestStyle:animated:forced:]+90>
                        <-[UIStatusBar _evaluateServerRegistration]+250>
                         <__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke+590>
                          <-[UIView(Hierarchy) _postMovedFromSuperview:]+544>
                           <-[UIView(Internal) _addSubview:positioned:relativeTo:]+1967>
                            <-[UIStatusBarWindow setStatusBar:]+288>
                             <-[UIApplication _createStatusBarWithRequestedStyle:orientation:hidden:]+340>
                              <-[UIApplication _runWithMainScene:transitionContext:completion:]+950>
                               <-[UIApplication workspaceDidEndTransaction:]+188>
                                <-[FBSSerialQueue _performNext]+192>
                                 <-[FBSSerialQueue _performNextFromRunLoopSource]+45>
                                  <__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__+17>
                                   <__CFRunLoopDoSources0+556>
                                    <__CFRunLoopRun+867>
                                     <CFRunLoopRunSpecific+488>
                                      <-[UIApplication _run]+402>
                                       <UIApplicationMain+171>
                                        <main+117>
    
    
    
    Aug  7 14:27:00  <Error>: CGContextTranslateCTM: invalid context 0x0. Backtrace:
          <-[UIStatusBarItemView updateContentsAndWidth]+33>
           <-[UIStatusBarItemView initWithItem:data:actions:style:]+477>
            <+[UIStatusBarItemView createViewForItem:withData:actions:foregroundStyle:]+134>
             <-[UIStatusBarLayoutManager _createViewForItem:withData:actions:]+163>
              <-[UIStatusBarLayoutManager _prepareEnabledItemType:withEnabledItems:withData:actions:itemAppearing:itemDisappearing:]+36
               <-[UIStatusBarLayoutManager prepareEnabledItems:withData:actions:]+92>
                <-[UIStatusBarForegroundView _setStatusBarData:actions:animated:]+797>
                 <-[UIStatusBarForegroundView setStatusBarData:actions:animated:]+332>
                  <__51-[UIStatusBar _prepareToSetStyle:animation:forced:]_block_invoke+360>
                   <+[UIView(Animation) performWithoutAnimation:]+65>
                    <-[UIStatusBar _prepareToSetStyle:animation:forced:]+866>
                     <-[UIStatusBar _requestStyleAttributes:animationParameters:forced:]+391>
                      <-[UIStatusBar requestStyle:animationParameters:forced:]+437>
                       <-[UIStatusBar requestStyle:animated:forced:]+90>
                        <-[UIStatusBar _evaluateServerRegistration]+250>
                         <__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke+590>
                          <-[UIView(Hierarchy) _postMovedFromSuperview:]+544>
                           <-[UIView(Internal) _addSubview:positioned:relativeTo:]+1967>
                            <-[UIStatusBarWindow setStatusBar:]+288>
                             <-[UIApplication _createStatusBarWithRequestedStyle:orientation:hidden:]+340>
                              <-[UIApplication _runWithMainScene:transitionContext:completion:]+950>
                               <-[UIApplication workspaceDidEndTransaction:]+188>
                                <-[FBSSerialQueue _performNext]+192>
                                 <-[FBSSerialQueue _performNextFromRunLoopSource]+45>
                                  <__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__+17>
                                   <__CFRunLoopDoSources0+556>
                                    <__CFRunLoopRun+867>
                                     <CFRunLoopRunSpecific+488>
                                      <-[UIApplication _run]+402>
                                       <UIApplicationMain+171>
                                        <main+117>
    
    
    
    
    Aug  7 14:27:00 <Error>: CGContextRestoreGState: invalid context 0x0. Backtrace:
          <-[UIStatusBarItemView updateContentsAndWidth]+33>
           <-[UIStatusBarItemView initWithItem:data:actions:style:]+477>
            <+[UIStatusBarItemView createViewForItem:withData:actions:foregroundStyle:]+134>
             <-[UIStatusBarLayoutManager _createViewForItem:withData:actions:]+163>
              <-[UIStatusBarLayoutManager _prepareEnabledItemType:withEnabledItems:withData:actions:itemAppearing:itemDisappearing:]+36
               <-[UIStatusBarLayoutManager prepareEnabledItems:withData:actions:]+92>
                <-[UIStatusBarForegroundView _setStatusBarData:actions:animated:]+797>
                 <-[UIStatusBarForegroundView setStatusBarData:actions:animated:]+332>
                  <__51-[UIStatusBar _prepareToSetStyle:animation:forced:]_block_invoke+360>
                   <+[UIView(Animation) performWithoutAnimation:]+65>
                    <-[UIStatusBar _prepareToSetStyle:animation:forced:]+866>
                     <-[UIStatusBar _requestStyleAttributes:animationParameters:forced:]+391>
                      <-[UIStatusBar requestStyle:animationParameters:forced:]+437>
                       <-[UIStatusBar requestStyle:animated:forced:]+90>
                        <-[UIStatusBar _evaluateServerRegistration]+250>
                         <__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke+590>
                          <-[UIView(Hierarchy) _postMovedFromSuperview:]+544>
                           <-[UIView(Internal) _addSubview:positioned:relativeTo:]+1967>
                            <-[UIStatusBarWindow setStatusBar:]+288>
                             <-[UIApplication _createStatusBarWithRequestedStyle:orientation:hidden:]+340>
                              <-[UIApplication _runWithMainScene:transitionContext:completion:]+950>
                               <-[UIApplication workspaceDidEndTransaction:]+188>
                                <-[FBSSerialQueue _performNext]+192>
                                 <-[FBSSerialQueue _performNextFromRunLoopSource]+45>
                                  <__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__+17>
                                   <__CFRunLoopDoSources0+556>
                                    <__CFRunLoopRun+867>
                                     <CFRunLoopRunSpecific+488>
                                      <-[UIApplication _run]+402>
                                       <UIApplicationMain+171>
                                        <main+117>
    
  • Admin
    Admin almost 9 years
    Thanks this shows the total backtrace of CG Context... Turns out, it has nothing todo with my buttons.. it's the UIStatusBarItemView..
  • utogaria
    utogaria almost 9 years
    Could you try to double check in your xib/storyboard file and see if there's any background image set for any of the customized UIButton, UIBarButtonItem, etc. If the image is not correctly set (image file missing, etc), it could also generate warnings like you have.
  • Admin
    Admin almost 9 years
    Double checked.. all images are setup correctly.. they are all in the Assets.xcassets I even removed the buttons.. same error keeps appearing..
  • utogaria
    utogaria almost 9 years
    There's another thing you can try. In Xcode, go to "breakpoint navigator", and add an "Exception breakpoint". Run the app, and see if it triggers any exception breakpoint. If so, you might be able to see deeper what happens.
  • Arpit B Parekh
    Arpit B Parekh over 8 years
    If it works then It is a very good think for debugging the error related to, UI parts.
  • Board
    Board over 8 years
    This bug still exists in Xcode 7.0 (7A220). It is easily reproducible by creating a new empty project and setting UIViewControllerBasedStatusBarAppearance to NO in info.plist.
  • samthui7
    samthui7 over 8 years
    In the mentioned link above, an Apple staff just answered: "Indeed, the root cause of the error messages is an issue with iOS when an app uses some status bar types, such as hidden.", and "You may ignore this warning."
  • swiftBoy
    swiftBoy over 8 years
    your solution worked for me too, however, what if I want the UIStatusbar to stay white?
  • mobibob
    mobibob over 8 years
    I have this problem in a Playground and I don't see any schemes, therefore, I cannot edit (or manage) the scheme. What am I misunderstanding?
  • superarts.org
    superarts.org over 8 years
    I wouldn't consider it's a "fix" since I thought most people who set UIViewControllerBasedStatusBarAppearance only because they needed it, but turned out I was wrong since this answer has 19 upvotes :p
  • Weles
    Weles over 8 years
    Yes, i agree with you. I found the solution to change status bar's color in this link: stackoverflow.com/a/32675066/1095530
  • thibaut noah
    thibaut noah over 8 years
    Not working here, and some people need this in the plist since the former way to hide the status bar is now deprecated
  • Mohamed Saleh
    Mohamed Saleh over 8 years
    I observe the same issue under iOS 9.2 and Xcode 7.2
  • esttorhe
    esttorhe about 8 years
    This doesn't make sense; UIViewControllerBaseStatusBarAppearance and View controller-based status bar appearance are the same thing on the Info.plist. One is the key value and the other the user facing text.
  • Thiha Aung
    Thiha Aung over 6 years
    That solved my problem after I have upgraded my old one to Xcode 9 for supporting iPhone X. I have to change storyboards to 9.0 or later to deployment target. That can't solve all. @utogaria, yours help me out at iOS 9.0 crash. May I know what's that and what it do?