--- old/src/java.desktop/macosx/classes/sun/awt/CGraphicsEnvironment.java 2018-10-15 17:30:24.000000000 -0700 +++ new/src/java.desktop/macosx/classes/sun/awt/CGraphicsEnvironment.java 2018-10-15 17:30:24.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,10 +25,22 @@ package sun.awt; -import java.awt.*; -import java.util.*; - -import sun.java2d.*; +import java.awt.AWTError; +import java.awt.Font; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; +import java.awt.HeadlessException; +import java.awt.Toolkit; +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; + +import sun.java2d.MacosxSurfaceManagerFactory; +import sun.java2d.SunGraphicsEnvironment; +import sun.java2d.SurfaceManagerFactory; /** * This is an implementation of a GraphicsEnvironment object for the default @@ -87,6 +99,9 @@ /** Reference to the display reconfiguration callback context. */ private final long displayReconfigContext; + // list of invalidated graphics devices (those which were removed) + private List> oldDevices = new ArrayList<>(); + /** * Construct a new instance. */ @@ -116,11 +131,23 @@ synchronized (this) { if (removed && devices.containsKey(displayId)) { final CGraphicsDevice gd = devices.remove(displayId); - gd.invalidate(getMainDisplayID()); - gd.displayChanged(); + oldDevices.add(new WeakReference<>(gd)); } } initDevices(); + + // Need to notify old devices, in case the user hold the reference to it + for (ListIterator> it = + oldDevices.listIterator(); it.hasNext(); ) { + CGraphicsDevice gd = it.next().get(); + if (gd != null) { + gd.invalidate(mainDisplayID); + gd.displayChanged(); + } else { + // no more references to this device, remove it + it.remove(); + } + } } @Override