More cautious image creation
This commit is contained in:
parent
e09faaf491
commit
1b3f66703f
@ -34,6 +34,8 @@ import com.sun.jna.ptr.PointerByReference;
|
||||
|
||||
public class HBITMAPWrap extends HBITMAP {
|
||||
|
||||
private static final Object lockObject = new Object();
|
||||
|
||||
// NOTE: This is a field (instead of private) so that GC does not try to collect this object
|
||||
private HBITMAP bitmap;
|
||||
|
||||
@ -42,6 +44,9 @@ public class HBITMAPWrap extends HBITMAP {
|
||||
HBITMAP createBitmap(BufferedImage image) {
|
||||
int w = image.getWidth(null);
|
||||
int h = image.getHeight(null);
|
||||
|
||||
// all sorts of issues occur if this is called quickly from different threads!
|
||||
synchronized(lockObject) {
|
||||
HDC screenDC = User32.GetDC(null);
|
||||
HDC memDC = GDI32.CreateCompatibleDC(screenDC);
|
||||
HBITMAP hBitmap = null;
|
||||
@ -92,6 +97,7 @@ public class HBITMAPWrap extends HBITMAP {
|
||||
GDI32.DeleteDC(memDC);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BufferedImage img;
|
||||
|
||||
|
@ -27,6 +27,7 @@ import dorkbox.jna.windows.structs.ICONINFO;
|
||||
* http://www.pinvoke.net/default.aspx/user32.createiconindirect
|
||||
*/
|
||||
public class HICONWrap extends HICON {
|
||||
private static final Object lockObject = new Object();
|
||||
|
||||
static HICON createIconIndirect(HBITMAP bm) {
|
||||
ICONINFO info = new ICONINFO();
|
||||
@ -35,9 +36,16 @@ public class HICONWrap extends HICON {
|
||||
info.ColorBitmap = bm;
|
||||
|
||||
HICON hicon = User32.CreateIconIndirect(info);
|
||||
if (hicon == null) {
|
||||
// something weird is going on! Try again but more carefully!
|
||||
synchronized(lockObject) {
|
||||
hicon = User32.CreateIconIndirect(info);
|
||||
}
|
||||
|
||||
if (hicon == null) {
|
||||
throw new GetLastErrorException();
|
||||
}
|
||||
}
|
||||
|
||||
return hicon;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user