Changeset 18388 in josm for trunk/src/org


Ignore:
Timestamp:
2022-03-08T15:52:28+01:00 (3 years ago)
Author:
taylor.smock
Message:

fix #21919: NPE in MapImage due to a race condition

This adds a synchronize block where disabledImgCache is calculated.
The new synchronize block will vie for the same lock as that in loadImage.
There should not be any significant UI penalty from this, as the loadImage
synchronized block does not depend upon anything particularly expensive, like
a network call. It also helps that almost all the time where both blocks will
be in contention, the new synchronize block will win.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java

    r17144 r18388  
    140140        if (img == null)
    141141            getImage(); // fix #7498 ?
    142         Image disImg = GuiHelper.getDisabledImage(img);
    143         if (disImg instanceof BufferedImage) {
    144             disabledImgCache = (BufferedImage) disImg;
    145         } else {
    146             disabledImgCache = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    147             Graphics g = disabledImgCache.getGraphics();
    148             g.drawImage(disImg, 0, 0, null);
    149             g.dispose();
     142        // This should fix #21919: NPE due to disabledImgCache being null (race condition with #loadImage())
     143        synchronized (this) {
     144            Image disImg = GuiHelper.getDisabledImage(img);
     145            if (disImg instanceof BufferedImage) {
     146                disabledImgCache = (BufferedImage) disImg;
     147            } else {
     148                disabledImgCache = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
     149                Graphics g = disabledImgCache.getGraphics();
     150                g.drawImage(disImg, 0, 0, null);
     151                g.dispose();
     152            }
    150153        }
    151154        return disabledImgCache;
Note: See TracChangeset for help on using the changeset viewer.