JDK-6831657 : NativeInLightFixer fails to removeReferences() and causes memory leak.
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6u14
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_vista
  • CPU: x86
  • Submitted: 2009-04-17
  • Updated: 2019-12-17
  • Resolved: 2019-12-17
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
Java 1.5 upwards

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.0.6001]

A DESCRIPTION OF THE PROBLEM :
When embedding heavyweight components within a JFrame and changing their Z-Order, the NativeInLightFixer fails to execute it's removeReferences()-method when removing a Panel from the JFrame. The resulting error is a NullPointerException at java.awt.Component$NativeInLightFixer.removeReferences. Line numbers depend on the Java-version.

Java 1.5.0_12: Component.java:7865
Java 1.6.0_3: Component.java:8563
Java 1.6.0_14: Component.java:8800

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
The basic steps to reproduce is, to create a JFrame, set it visible, add two Panels, change the Z-Order of one and then try to remove this one.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The expected result should be, that the component is removed from the parent and no error is being thrown.
ACTUAL -
It will result in the described NullPointerException.

However, you can workaround this problem, by catching the Exception. The Component will be removed (optically) but a reference of NativeInLightFixer will still point at the component, since it registers itself as a ComponentListener with the component and is unable to unregister itself due to the NullPointer, thus resulting in a memory leak.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
With the newest Java Version (1.6.0_14) the stacktrace of the error is as follows:
Exception in thread "main" java.lang.NullPointerException
	at java.awt.Component$NativeInLightFixer.removeReferences(Component.java:8800)
	at java.awt.Component$NativeInLightFixer.componentRemoved(Component.java:8776)
	at java.awt.AWTEventMulticaster.componentRemoved(AWTEventMulticaster.java:193)
	at java.awt.Container.processContainerEvent(Container.java:2070)
	at java.awt.Container.processEvent(Container.java:2038)
	at java.awt.Component.dispatchEventImpl(Component.java:4604)
	at java.awt.Container.dispatchEventImpl(Container.java:2099)
	at java.awt.Component.dispatchEvent(Component.java:4434)
	at java.awt.Container.remove(Container.java:1162)
	at java.awt.Container.remove(Container.java:1198)
	at LightFixerTest.main(LightFixerTest.java:31)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Panel;

import javax.swing.JFrame;

public class LightFixerTest
{
  public static void main(String[] args)
  {
    JFrame f = new JFrame();
    Dimension d = new Dimension(100, 100);
    f.setSize(d);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);

    Panel p1 = new Panel();
    p1.setBackground(Color.black);
    p1.setSize(d);

    Panel p2 = new Panel();
    p2.setBackground(Color.white);
    p2.setSize(d);

    f.getContentPane().add(p1, 0);
    f.getContentPane().add(p2, 1);

    f.getContentPane().setComponentZOrder(p2, 0);

    f.getContentPane().remove(p2);
  }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
There is no workaround per-se around this problem, only the trick to catch the exception. This, however, results in a memory leak (see "actual result"-section)

Comments
EVALUATION Could the fix indicated above be applied to the nearest JDK 6 update? There are more than 40 reports of the NPE gathered from NetBeans users so far: http://statistics.netbeans.org/exceptions/detail.do?id=187182 Thanks.
14-09-2012

EVALUATION Backporting the changes for 4993545 to 6 update would fix the bug.
17-06-2009

EVALUATION assigned to AWT It doesn't seem to be a threading problem, the exception persists if the code is called from the EDT
15-05-2009