Bug 576621: Review disposal of SWT resources
Change-Id: I3b4c61e1ca840f00b30cc2d7f09affcf9a8ea539
diff --git a/eclient/org.eclipse.statet.rj.eclient.graphics/src/org/eclipse/statet/internal/rj/eclient/graphics/EclipseRGraphic.java b/eclient/org.eclipse.statet.rj.eclient.graphics/src/org/eclipse/statet/internal/rj/eclient/graphics/EclipseRGraphic.java
index 7e8d549..4e2c404 100644
--- a/eclient/org.eclipse.statet.rj.eclient.graphics/src/org/eclipse/statet/internal/rj/eclient/graphics/EclipseRGraphic.java
+++ b/eclient/org.eclipse.statet.rj.eclient.graphics/src/org/eclipse/statet/internal/rj/eclient/graphics/EclipseRGraphic.java
@@ -14,6 +14,8 @@
package org.eclipse.statet.internal.rj.eclient.graphics;
+import static org.eclipse.statet.ecommons.ui.swt.AutoDisposeReference.autoDispose;
+
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -105,14 +107,14 @@
for (int i= beginIdx; i < endIdx; i++) {
switch (instructions[i].getInstructionType()) {
case RGraphicInstruction.DRAW_RASTER: {
- final Image image= ((RasterElement) instructions[i]).swtImage;
- if (image != null && !image.isDisposed()) {
+ final Image image= ((RasterElement)instructions[i]).swtImage;
+ if (image != null) {
image.dispose();
}
continue; }
case RGraphicInstruction.DRAW_PATH: {
final Path path= ((PathElement) instructions[i]).swtPath;
- if (path != null && !path.isDisposed()) {
+ if (path != null) {
path.dispose();
}
continue; }
@@ -855,42 +857,33 @@
@Override
public byte[] capture(final int width, final int height) {
- ImageData imageData;
- { Image image= null;
- GC gc= null;
+ final ImageData imageData;
+ try (final var managedImage= autoDispose(new Image(this.display, width, height))) {
+ final DefaultGCRenderer renderer= new DefaultGCRenderer();
+ final List<ERGraphicInstruction> instructions= getCurrentInstructions();
+ if (instructions.isEmpty()) {
+ return null;
+ }
+ { final RGraphicInitialization init= (RGraphicInitialization) instructions.get(0);
+ double scale;
+ if (width == (int) (init.width + 0.5)) {
+ scale= 1.0;
+ }
+ else {
+ scale= width / init.width;
+ }
+ renderer.clear(scale);
+ }
+
+ final GC gc= new GC(managedImage.get());
try {
- image= new Image(this.display, width, height);
- gc= new GC(image);
- final DefaultGCRenderer renderer= new DefaultGCRenderer();
- final List<ERGraphicInstruction> instructions= getCurrentInstructions();
- if (instructions.isEmpty()) {
- return null;
- }
- { final RGraphicInitialization init= (RGraphicInitialization) instructions.get(0);
- double scale;
- if (width == (int) (init.width + 0.5)) {
- scale= 1.0;
- }
- else {
- scale= width / init.width;
- }
- renderer.clear(scale);
- }
renderer.paint(gc, instructions);
- gc.dispose();
- gc= null;
- imageData= image.getImageData();
- image.dispose();
- image= null;
}
finally {
- if (gc != null && !gc.isDisposed()) {
- gc.dispose();
- }
- if (image != null && !image.isDisposed()) {
- image.dispose();
- }
+ gc.dispose();
}
+
+ imageData= managedImage.get().getImageData();
}
if (imageData == null || !imageData.palette.isDirect) {
return null;
diff --git a/eclient/org.eclipse.statet.rj.eclient.graphics/src/org/eclipse/statet/internal/rj/eclient/graphics/FontManager.java b/eclient/org.eclipse.statet.rj.eclient.graphics/src/org/eclipse/statet/internal/rj/eclient/graphics/FontManager.java
index 4d56ccb..5817845 100644
--- a/eclient/org.eclipse.statet.rj.eclient.graphics/src/org/eclipse/statet/internal/rj/eclient/graphics/FontManager.java
+++ b/eclient/org.eclipse.statet.rj.eclient.graphics/src/org/eclipse/statet/internal/rj/eclient/graphics/FontManager.java
@@ -14,6 +14,8 @@
package org.eclipse.statet.internal.rj.eclient.graphics;
+import static org.eclipse.statet.ecommons.ui.swt.AutoDisposeReference.autoDispose;
+
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -58,7 +60,7 @@
public final int size;
public final Font swtFont;
- private double[] swtFontProperties; // [ baselineOffset ] -> FontSetting
+ private double @Nullable [] swtFontProperties; // [ baselineOffset ] -> FontSetting
private int baseLine;
private double metricTolerance= 0;
@@ -136,13 +138,19 @@
public TestGC(final Device device) {
- final GC tempGC= new GC(device);
- final Font tempFont= new Font(device, new FontData(device.getSystemFont().getFontData()[0].getName(), HR_FONTSIZE, 0));
- tempGC.setFont(tempFont);
- this.imageHeigth= (int) (tempGC.getFontMetrics().getHeight() * 1.5);
- this.imageWidth= this.imageHeigth * 2;
- tempGC.dispose();
- tempFont.dispose();
+ // image size
+ try (final var managedFont= autoDispose(new Font(device,
+ new FontData(device.getSystemFont().getFontData()[0].getName(), HR_FONTSIZE, 0) ))) {
+ final GC gc= new GC(device);
+ try {
+ gc.setFont(managedFont.get());
+ this.imageHeigth= (int)(gc.getFontMetrics().getHeight() * 1.5);
+ this.imageWidth= this.imageHeigth * 2;
+ }
+ finally {
+ gc.dispose();
+ }
+ }
this.image= new Image(device, this.imageWidth, this.imageHeigth);
@@ -231,12 +239,8 @@
public void dispose() {
- if (!this.testGC.isDisposed()) {
- this.testGC.dispose();
- }
- if (!this.image.isDisposed()) {
- this.image.dispose();
- }
+ this.testGC.dispose();
+ this.image.dispose();
}
}
@@ -485,13 +489,15 @@
public synchronized double[] getSWTFontProperties(final int style, final int size) {
final FontInstance font= get(style, size); // no HR!
- if (font.swtFontProperties == null) {
+ var fontProperties= font.swtFontProperties;
+ if (fontProperties == null) {
synchronized (getTestLock()) {
final TestGC gc= getTestGC();
gc.setFont(font);
}
+ fontProperties= font.swtFontProperties;
}
- return font.swtFontProperties;
+ return fontProperties;
}
private double polish(final double p, final double factor) {