From 26f48c225e3ac397ec1ba5de2e135d630dbc2e4b Mon Sep 17 00:00:00 2001 From: Tim Kosse Date: Mon, 4 Dec 2017 13:15:44 +0100 Subject: [PATCH] Fix crash when switching between monitors in wxOSX It is possible for CGDisplayModeCopyPixelEncoding to return NULL, e.g. during a switch between the built-in screen and an external monitor on a Macbook. Check the return value to prevent a crash. Closes #18015. --- src/osx/utils_osx.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/osx/utils_osx.cpp b/src/osx/utils_osx.cpp index 47e07ba683..20983f98de 100644 --- a/src/osx/utils_osx.cpp +++ b/src/osx/utils_osx.cpp @@ -66,21 +66,22 @@ bool wxColourDisplay() // Returns depth of screen int wxDisplayDepth() { - int theDepth = 0; - CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay); CFStringRef encoding = CGDisplayModeCopyPixelEncoding(currentMode); - - if(CFStringCompare(encoding, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) - theDepth = 32; - else if(CFStringCompare(encoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) - theDepth = 16; - else if(CFStringCompare(encoding, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) - theDepth = 8; - else - theDepth = 32; // some reasonable default - CFRelease(encoding); + int theDepth = 32; // some reasonable default + if(encoding) + { + if(CFStringCompare(encoding, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) + theDepth = 32; + else if(CFStringCompare(encoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) + theDepth = 16; + else if(CFStringCompare(encoding, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) + theDepth = 8; + + CFRelease(encoding); + } + CGDisplayModeRelease(currentMode); return theDepth;