diff --git a/src/java.desktop/macosx/classes/apple/laf/JRSUIControl.java b/src/java.desktop/macosx/classes/apple/laf/JRSUIControl.java index f1f5ebe5ff166..f28e05d1877de 100644 --- a/src/java.desktop/macosx/classes/apple/laf/JRSUIControl.java +++ b/src/java.desktop/macosx/classes/apple/laf/JRSUIControl.java @@ -27,6 +27,8 @@ import java.nio.*; import java.util.*; +import sun.java2d.Disposer; +import sun.java2d.DisposerRecord; import apple.laf.JRSUIConstants.*; @@ -91,7 +93,8 @@ private static ThreadLocalByteBuffer getThreadLocalBuffer() { private final HashMap nativeMap; private final HashMap changes; - private long cfDictionaryPtr; + private final long cfDictionaryPtr; + private final Object disposerReferent = new Object(); private long priorEncodedProperties; private long currentEncodedProperties; @@ -101,6 +104,7 @@ public JRSUIControl(final boolean flipped){ this.flipped = flipped; cfDictionaryPtr = getCFDictionary(flipped); if (cfDictionaryPtr == 0) throw new RuntimeException("Unable to create native representation"); + Disposer.addRecord(disposerReferent, new JRSUIControlDisposerRecord(cfDictionaryPtr)); nativeMap = new HashMap(); changes = new HashMap(); } @@ -109,17 +113,25 @@ public JRSUIControl(final boolean flipped){ flipped = other.flipped; cfDictionaryPtr = getCFDictionary(flipped); if (cfDictionaryPtr == 0) throw new RuntimeException("Unable to create native representation"); + Disposer.addRecord(disposerReferent, new JRSUIControlDisposerRecord(cfDictionaryPtr)); nativeMap = new HashMap(); changes = new HashMap(other.nativeMap); changes.putAll(other.changes); } - @Override - @SuppressWarnings("removal") - protected synchronized void finalize() throws Throwable { - if (cfDictionaryPtr == 0) return; - disposeCFDictionary(cfDictionaryPtr); - cfDictionaryPtr = 0; + private static class JRSUIControlDisposerRecord implements DisposerRecord { + + private final long cfDictionaryPtr; + JRSUIControlDisposerRecord(long ptr) { + cfDictionaryPtr = ptr; + } + + public void dispose() { + try { + disposeCFDictionary(cfDictionaryPtr); + } catch (Throwable t) { + } + } }