Skip to content

Commit 31ba6b8

Browse files
committed
Fix memory-leak in FigureUtilities by disposing GC on font change
FigureUtilities create a GC once for an application and then reuse it to calculate font/text dimensions. Due to the modification of GC to store Operations, every operation on FigureUtilities that sets the font on the GC (i.e., that calls setFont()) creates a SetFontOperation in the GC. Since that GC is never dispose, these operation accumulate throughout the application lifecycle and constituted a memory leak The current change addresses this issue by creating a single shell for the lifetime of the application. Whenever a different font needs to be set, the old GC is properly disposed and replaced with a new one created from the shell. This ensures that no GC objects persist indefinitely, effectively preventing the memory leak. The now-unused getGC() method which was protected has been removed from the codebase as part of this change.
1 parent 8726ed7 commit 31ba6b8

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

org.eclipse.draw2d/src/org/eclipse/draw2d/FigureUtilities.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class FigureUtilities {
3232

3333
private static final float RGB_VALUE_MULTIPLIER = 0.6f;
3434
private static GC gc;
35+
private static Shell shell;
3536
private static Font appliedFont;
3637
private static FontMetrics metrics;
3738
private static Color ghostFillColor = new Color(null, 31, 31, 31);
@@ -59,29 +60,19 @@ public static Color darker(Color color) {
5960
public static FontMetrics getFontMetrics(Font f) {
6061
setFont(f);
6162
if (metrics == null) {
62-
metrics = getGC().getFontMetrics();
63+
metrics = gc.getFontMetrics();
6364
}
6465
return metrics;
6566
}
6667

67-
/**
68-
* Returns the GC used for various utilities. Advanced graphics must not be
69-
* switched on by clients using this GC.
70-
*
71-
* @deprecated do not mess with this GC
72-
* @return the GC
73-
*/
74-
@Deprecated
75-
protected static GC getGC() {
76-
if (gc == null) {
68+
private static Shell getShell() {
69+
if (shell == null) {
7770
Shell shell = new Shell();
7871
InternalDraw2dUtils.configureForAutoscalingMode(shell, event -> {
7972
// ignored
8073
});
81-
gc = new GC(shell);
82-
appliedFont = gc.getFont();
8374
}
84-
return gc;
75+
return shell;
8576
}
8677

8778
/**
@@ -95,7 +86,7 @@ protected static GC getGC() {
9586
*/
9687
protected static org.eclipse.swt.graphics.Point getTextDimension(String s, Font f) {
9788
setFont(f);
98-
return getGC().textExtent(s);
89+
return gc.textExtent(s);
9990
}
10091

10192
/**
@@ -123,7 +114,7 @@ public static IFigure getRoot(IFigure figure) {
123114
*/
124115
protected static org.eclipse.swt.graphics.Point getStringDimension(String s, Font f) {
125116
setFont(f);
126-
return getGC().stringExtent(s);
117+
return gc.stringExtent(s);
127118
}
128119

129120
/**
@@ -341,10 +332,14 @@ public static void paintEtchedBorder(Graphics g, Rectangle r) {
341332
* @since 2.0
342333
*/
343334
protected static void setFont(Font f) {
344-
if (appliedFont == f || (f != null && f.equals(appliedFont))) {
335+
if ((appliedFont == null && f == null && gc != null) || (f != null && f.equals(appliedFont))) {
345336
return;
346337
}
347-
getGC().setFont(f);
338+
if (gc != null && !gc.isDisposed()) {
339+
gc.dispose();
340+
}
341+
gc = new GC(getShell());
342+
gc.setFont(f);
348343
appliedFont = f;
349344
metrics = null;
350345
}

0 commit comments

Comments
 (0)