How can I set CG_CONTEXT_SHOW_BACKTRACE environmental variable?
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:
Admin
Updated on February 15, 2020Comments
-
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 almost 9 yearsThanks this shows the total backtrace of CG Context... Turns out, it has nothing todo with my buttons.. it's the UIStatusBarItemView..
-
utogaria almost 9 yearsCould 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 almost 9 yearsDouble checked.. all images are setup correctly.. they are all in the Assets.xcassets I even removed the buttons.. same error keeps appearing..
-
utogaria almost 9 yearsThere'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 over 8 yearsIf it works then It is a very good think for debugging the error related to, UI parts.
-
Board over 8 yearsThis 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 over 8 yearsIn 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 over 8 yearsyour solution worked for me too, however, what if I want the UIStatusbar to stay white?
-
mobibob over 8 yearsI 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 over 8 yearsI 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 over 8 yearsYes, i agree with you. I found the solution to change status bar's color in this link: stackoverflow.com/a/32675066/1095530
-
thibaut noah over 8 yearsNot working here, and some people need this in the plist since the former way to hide the status bar is now deprecated
-
Mohamed Saleh over 8 yearsI observe the same issue under
iOS 9.2
andXcode 7.2
-
esttorhe about 8 yearsThis doesn't make sense;
UIViewControllerBaseStatusBarAppearance
andView controller-based status bar appearance
are the same thing on theInfo.plist
. One is the key value and the other the user facing text. -
Thiha Aung over 6 yearsThat 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?