RFR: 4200096: OffScreenImageSource.removeConsumer NullPointerException

Jeremy duke at openjdk.org
Tue Apr 11 03:49:27 UTC 2023


On Mon, 10 Apr 2023 16:16:32 GMT, Jeremy <duke at openjdk.org> wrote:

> This resolves a 25 year old P4 ticket: a NullPointerException is printed to System.err needlessly.
> 
> This resolution involves confirming that an ImageConsumer is still registered before every notification.
> 
> I'll understand if this is rejected as unimportant, but I stumbled across this in the real world the other day and thought this was a simple enough bug to practice on.

Thanks for the thorough review.

To recap:

Yes, the original problem (as I understand it) has to do with listeners that detach mid-production.

I apologize if I failed to explain this up-front. I tried to describe the problem I set out to "solve" in the comments preceding the first unit test:

https://github.com/mickleness/jdk/blob/73e9f010b3c356c2cf405855f5a33f387e7bb7ee/test/jdk/sun/awt/image/OffScreenImageSource/bug4200096.java#L57-L91

> I'd like to know what stack trace you see.

In the master openjdk branch this test fails as follows:


----------System.err:(29/2496)----------
java.lang.NullPointerException: Cannot invoke "java.awt.image.ImageConsumer.setProperties(java.util.Hashtable)" because "this.theConsumer" is null
java.lang.RuntimeException: java.lang.NullPointerException: Cannot invoke "java.awt.image.ImageConsumer.setProperties(java.util.Hashtable)" because "this.theConsumer" is null
	at bug4200096$1.println(bug4200096.java:49)
	at java.base/java.lang.Throwable$WrappedPrintStream.println(Throwable.java:785)
	at java.base/java.lang.Throwable.lockedPrintStackTrace(Throwable.java:684)
	at java.base/java.lang.Throwable.printStackTrace(Throwable.java:673)
	at java.base/java.lang.Throwable.printStackTrace(Throwable.java:660)
	at java.base/java.lang.Throwable.printStackTrace(Throwable.java:651)
	at java.desktop/sun.awt.image.OffScreenImageSource.produce(OffScreenImageSource.java:204)
	at java.desktop/sun.awt.image.OffScreenImageSource.addConsumer(OffScreenImageSource.java:66)
	at java.desktop/sun.awt.image.OffScreenImageSource.startProduction(OffScreenImageSource.java:80)
	at java.desktop/java.awt.image.FilteredImageSource.startProduction(FilteredImageSource.java:184)
	at java.desktop/sun.awt.image.ImageRepresentation.startProduction(ImageRepresentation.java:732)
	at java.desktop/sun.awt.image.ToolkitImage.addWatcher(ToolkitImage.java:221)
	at java.desktop/sun.awt.image.ToolkitImage.getWidth(ToolkitImage.java:110)
	at bug4200096.runImageDimensionTest(bug4200096.java:90)
	at bug4200096.main(bug4200096.java:53)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at com.sun.javatest.regtest.agent.MainWrapper$MainTask.run(MainWrapper.java:138)
	at java.base/java.lang.Thread.run(Thread.java:1623)
Caused by: java.lang.NullPointerException: Cannot invoke "java.awt.image.ImageConsumer.setProperties(java.util.Hashtable)" because "this.theConsumer" is null
	at java.desktop/sun.awt.image.OffScreenImageSource.produce(OffScreenImageSource.java:186)
	... 12 more


So in this test: I'm not explicitly adding or removing an ImageConsumer or ImageObserver. My implementation of `ImageObserver#imageUpdate` is returning `false` once it received the dimensions. Returning false prompts something else to remove an ImageConsumer on my behalf, which resulted in the NPE in System.err when the OffScreenImageSource proceeded to keep producing the image.

I just pushed a few revisions that, among other things, pass in the ImageConsumer as an argument as you suggested. (But they also still constantly check `isConsumer(ImageObserver)`

-------------

PR Comment: https://git.openjdk.org/jdk/pull/13408#issuecomment-1502645900



More information about the client-libs-dev mailing list