Skip to content

Commit 239428b

Browse files
arunjose696HeikoKlare
authored andcommitted
Track and dispose resources created by operations in GC
Each Operation subclass now maintains its own list of disposable SWT resources. These are added to a `disposables` list within the Operation and are disposed when the GC is disposed. This ensures cleanup of resources.
1 parent 69a577c commit 239428b

File tree

1 file changed

+65
-18
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+65
-18
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,14 +1958,20 @@ private class DrawPathOperation extends Operation {
19581958
@Override
19591959
void apply() {
19601960
Path path = new Path(device, pathData);
1961-
long pathHandle = path.getHandle(getZoom());
1962-
if (pathHandle == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1963-
initGdip();
1964-
checkGC(DRAW);
1965-
long gdipGraphics = data.gdipGraphics;
1966-
Gdip.Graphics_TranslateTransform(gdipGraphics, data.gdipXOffset, data.gdipYOffset, Gdip.MatrixOrderPrepend);
1967-
Gdip.Graphics_DrawPath(gdipGraphics, data.gdipPen, pathHandle);
1968-
Gdip.Graphics_TranslateTransform(gdipGraphics, -data.gdipXOffset, -data.gdipYOffset, Gdip.MatrixOrderPrepend);
1961+
try {
1962+
long pathHandle = path.getHandle(getZoom());
1963+
if (pathHandle == 0)
1964+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
1965+
initGdip();
1966+
checkGC(DRAW);
1967+
long gdipGraphics = data.gdipGraphics;
1968+
Gdip.Graphics_TranslateTransform(gdipGraphics, data.gdipXOffset, data.gdipYOffset, Gdip.MatrixOrderPrepend);
1969+
Gdip.Graphics_DrawPath(gdipGraphics, data.gdipPen, pathHandle);
1970+
Gdip.Graphics_TranslateTransform(gdipGraphics, -data.gdipXOffset, -data.gdipYOffset,
1971+
Gdip.MatrixOrderPrepend);
1972+
} finally {
1973+
path.dispose();
1974+
}
19691975
}
19701976
}
19711977

@@ -3272,13 +3278,18 @@ private class FillPathOperation extends Operation {
32723278
@Override
32733279
void apply() {
32743280
Path path = new Path(device, pathData);
3275-
long pathHandle = path.getHandle(getZoom());
3276-
if (pathHandle == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3277-
initGdip();
3278-
checkGC(FILL);
3279-
int mode = OS.GetPolyFillMode(handle) == OS.WINDING ? Gdip.FillModeWinding : Gdip.FillModeAlternate;
3280-
Gdip.GraphicsPath_SetFillMode(pathHandle, mode);
3281-
Gdip.Graphics_FillPath(data.gdipGraphics, data.gdipBrush, pathHandle);
3281+
try {
3282+
long pathHandle = path.getHandle(getZoom());
3283+
if (pathHandle == 0)
3284+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3285+
initGdip();
3286+
checkGC(FILL);
3287+
int mode = OS.GetPolyFillMode(handle) == OS.WINDING ? Gdip.FillModeWinding : Gdip.FillModeAlternate;
3288+
Gdip.GraphicsPath_SetFillMode(pathHandle, mode);
3289+
Gdip.Graphics_FillPath(data.gdipGraphics, data.gdipBrush, pathHandle);
3290+
} finally {
3291+
path.dispose();
3292+
}
32823293
}
32833294
}
32843295

@@ -4629,7 +4640,9 @@ private class SetBackgroundOperation extends Operation {
46294640
private final Color color;
46304641

46314642
SetBackgroundOperation(Color color) {
4632-
this.color = color;
4643+
RGB rgb = color.getRGB();
4644+
this.color = new Color(color.getDevice(), rgb);
4645+
registerForDisposal(this.color);
46334646
}
46344647

46354648
@Override
@@ -4676,6 +4689,7 @@ private class SetBackgroundPatternOperation extends Operation {
46764689

46774690
SetBackgroundPatternOperation(Pattern pattern) {
46784691
this.pattern = pattern == null ? null : pattern.copy();
4692+
registerForDisposal(this.pattern);
46794693
}
46804694

46814695
@Override
@@ -4938,7 +4952,8 @@ private class SetFontOperation extends Operation {
49384952
private final Font font;
49394953

49404954
SetFontOperation(Font font) {
4941-
this.font = font;
4955+
this.font = new Font(font.getDevice(), font.getFontData());
4956+
registerForDisposal(this.font);
49424957
}
49434958

49444959
@Override
@@ -4973,7 +4988,9 @@ private class SetForegroundOperation extends Operation {
49734988
private final Color color;
49744989

49754990
SetForegroundOperation(Color color) {
4976-
this.color = color;
4991+
RGB rgb = color.getRGB();
4992+
this.color = new Color(color.getDevice(), rgb);
4993+
registerForDisposal(this.color);
49774994
}
49784995

49794996
@Override
@@ -5019,6 +5036,7 @@ private class SetForegroundPatternOperation extends Operation {
50195036

50205037
SetForegroundPatternOperation(Pattern pattern) {
50215038
this.pattern = pattern == null ? null : pattern.copy();
5039+
registerForDisposal(this.pattern);
50225040
}
50235041

50245042
@Override
@@ -5604,6 +5622,7 @@ private class SetTransformOperation extends Operation {
56045622
float[] elements = new float[6];
56055623
transform.getElements(elements);
56065624
this.transform = new Transform(device, elements[0], elements[1], elements[2], elements[3], elements[4], elements[5]);
5625+
registerForDisposal(this.transform);
56075626
} else {
56085627
this.transform = null;
56095628
}
@@ -5876,8 +5895,36 @@ private void createGcHandle(Drawable drawable, GCData newData, int nativeZoom) {
58765895
}
58775896
}
58785897

5898+
5899+
@Override
5900+
public void dispose() {
5901+
super.dispose();
5902+
disposeOperations();
5903+
}
5904+
5905+
private void disposeOperations() {
5906+
for (Operation op : operations) {
5907+
op.disposeAll();
5908+
}
5909+
operations.clear();
5910+
}
5911+
58795912
private abstract class Operation {
5913+
private final List<Resource> disposables = new ArrayList<>();
58805914
abstract void apply();
5915+
5916+
protected void registerForDisposal(Resource resource) {
5917+
if (resource != null) {
5918+
disposables.add(resource);
5919+
}
5920+
}
5921+
5922+
void disposeAll() {
5923+
for (Resource r : disposables) {
5924+
r.dispose();
5925+
}
5926+
disposables.clear();
5927+
}
58815928
}
58825929
}
58835930

0 commit comments

Comments
 (0)