It’s possible for this to crash if the image can’t be created. This patch just guards against the crash:
diff --git a/modules/juce_gui_basics/native/juce_Windowing_mac.mm b/modules/juce_gui_basics/native/juce_Windowing_mac.mm
index 6a1cc594df..8851f696e3 100644
--- a/modules/juce_gui_basics/native/juce_Windowing_mac.mm
+++ b/modules/juce_gui_basics/native/juce_Windowing_mac.mm
@@ -525,9 +525,10 @@ static Image createNSWindowSnapshot (NSWindow* nsWindow)
{
JUCE_AUTORELEASEPOOL
{
- const auto createImageFromCGImage = [&] (CGImageRef cgImage)
+ const auto createImageFromCGImage = [&] (CGImageRef cgImage) -> Image
{
- jassert (cgImage != nullptr);
+ if (cgImage == nullptr)
+ return {};
const auto width = CGImageGetWidth (cgImage);
const auto height = CGImageGetHeight (cgImage);
@@ -612,10 +613,16 @@ static Image createNSWindowSnapshot (NSWindow* nsWindow)
#else
JUCE_BEGIN_IGNORE_DEPRECATION_WARNINGS
- return createImageFromCGImage ((CGImageRef) CFAutorelease (CGWindowListCreateImage (CGRectNull,
- kCGWindowListOptionIncludingWindow,
- (CGWindowID) [nsWindow windowNumber],
- kCGWindowImageBoundsIgnoreFraming)));
+ // N.B. CGWindowListCreateImage returns null on macOS versions where this
+ // deprecated API is restricted (e.g. without Screen Recording permission).
+ // Passing null to CFAutorelease aborts in CoreFoundation, so guard it.
+ if (auto cgImage = CGWindowListCreateImage (CGRectNull,
+ kCGWindowListOptionIncludingWindow,
+ (CGWindowID) [nsWindow windowNumber],
+ kCGWindowImageBoundsIgnoreFraming))
+ return createImageFromCGImage ((CGImageRef) CFAutorelease (cgImage));
+
+ return {};
JUCE_END_IGNORE_DEPRECATION_WARNINGS
#endif
