RFR: 8055461: getNextID in ImageIcon class can lead to overflow

Alexey Ivanov aivanov at openjdk.org
Wed Jun 11 12:49:30 UTC 2025


On Wed, 11 Jun 2025 12:37:25 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:

> Something like this
> 
> ```
> ImageIcon icon = new ImageIcon();
>                 Image image = Toolkit.getDefaultToolkit().createImage("onepixel.gif");
>                 icon.setImage(image);
> if (icon.getImageLoadStatus == MediaTracker.ABORTED) {
>    ImageIcon newIcon = new ImageIcon();
> }
> ```
> 
> in this case wont mediaTrackerId starts from 0 again for "newIcon"?

No, it won't. Why will it?

`mediaTrackerId` is a static field, that is its value grows with each image loaded by a new instance of `ImageIcon`, and the value of `mediaTrackerId` is never reset. That's exactly the problem described in the [JDK-8055461](https://bugs.openjdk.org/browse/JDK-8055461) bug report.

https://github.com/openjdk/jdk/blob/bf7d40d0486b7b4e4820bb5d08a63c446ea3291d/src/java.desktop/share/classes/javax/swing/ImageIcon.java#L129

https://github.com/openjdk/jdk/blob/bf7d40d0486b7b4e4820bb5d08a63c446ea3291d/src/java.desktop/share/classes/javax/swing/ImageIcon.java#L322-L326

With the change that you propose, it will be impossible to load images with `ImageIcon` once the value of `mediaTrackerId` reaches `Integer.MAX_VALUE`.

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

PR Comment: https://git.openjdk.org/jdk/pull/25666#issuecomment-2962554490


More information about the client-libs-dev mailing list