Index: ide/applemenu/src/org/netbeans/modules/applemenu/FontReferenceQueue.java =================================================================== RCS file: ide/applemenu/src/org/netbeans/modules/applemenu/FontReferenceQueue.java diff -N ide/applemenu/src/org/netbeans/modules/applemenu/FontReferenceQueue.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ide/applemenu/src/org/netbeans/modules/applemenu/FontReferenceQueue.java 11 Apr 2005 22:57:23 -0000 @@ -0,0 +1,106 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.netbeans.modules.applemenu; + +import java.awt.Font; +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.lang.reflect.Field; + +// #57664: Random crash on Mac OS X. Apple JDK apparently has a bug in +// the native part of the font handling code. Font instances seems to share +// native data structures which are released when the instances are finalized. +// Because the native data are shared, their premature release causes random +// JVM crashes. +// +// This evil hack forces Font.finalize() not to run. The native font data +// are leaked but it's still better than total crash + +class FontReferenceQueue extends ReferenceQueue { + /** + * Polls this queue to see if a reference object is available. If one is + * available without further delay then it is removed from the queue and + * returned. Otherwise this method immediately returns null. + * + * @return A reference object, if one was immediately available, + * otherwise null + */ + public Reference poll() { + Reference ref; + + for (;;) { + ref = super.poll(); + if (ref == null) + break; + + Object obj = ref.get(); + if (! (obj instanceof Font)) + break; + } + return ref; + } + + /** + * Removes the next reference object in this queue, blocking until either + * one becomes available or the given timeout period expires. + * + *

This method does not offer real-time guarantees: It schedules the + * timeout as if by invoking the {@link Object#wait(long)} method. + * + * @param timeout If positive, block for up timeout + * milliseconds while waiting for a reference to be + * added to this queue. If zero, block indefinitely. + * + * @return A reference object, if one was available within the specified + * timeout period, otherwise null + * + * @throws IllegalArgumentException + * If the value of the timeout argument is negative + * + * @throws InterruptedException + * If the timeout wait is interrupted + */ + public Reference remove(long timeout) throws IllegalArgumentException, InterruptedException { + // timeout is not handled correctly, but good enough for our purposes + + Reference ref; + + for (;;) { + ref = super.remove(timeout); + if (ref == null) + break; + + Object obj = ref.get(); + if (! (obj instanceof Font)) + break; + } + return ref; + } + + static void install() { + try { + Class clzz = Class.forName("java.lang.ref.Finalizer"); // NOI18N + Field fld = clzz.getDeclaredField("queue"); // NOI18N + fld.setAccessible(true); + fld.set(null, new FontReferenceQueue()); + } catch (NoClassDefFoundError ex) { + // ex.printStackTrace(); + } catch (ClassNotFoundException ex) { + // ex.printStackTrace(); + } catch (Exception ex) { + // ex.printStackTrace(); + } + + } +} Index: ide/applemenu/src/org/netbeans/modules/applemenu/Install.java =================================================================== RCS file: /cvs/ide/applemenu/src/org/netbeans/modules/applemenu/Install.java,v retrieving revision 1.1 diff -u -u -r1.1 Install.java --- ide/applemenu/src/org/netbeans/modules/applemenu/Install.java 8 Jun 2004 12:46:10 -0000 1.1 +++ ide/applemenu/src/org/netbeans/modules/applemenu/Install.java 11 Apr 2005 22:57:23 -0000 @@ -7,7 +7,7 @@ * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original - * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun * Microsystems, Inc. All Rights Reserved. */ @@ -25,6 +25,8 @@ public class Install extends ModuleInstall { public void restored () { if (System.getProperty("mrj.version") != null) { // NOI18N + FontReferenceQueue.install(); + try { Class adapter = Class.forName("org.netbeans.modules.applemenu.NbApplicationAdapter"); Method m = adapter.getDeclaredMethod("install", new Class[0] );