Skip to content

Commit 29a71d3

Browse files
author
zhourenjian
committed
Refactor to remove element safely, especially avoiding memory leak in IE6
1 parent cdb1841 commit 29a71d3

26 files changed

Lines changed: 177 additions & 133 deletions

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/custom/CLabel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ public void redraw() {
394394
}
395395
} else {
396396
if (imageHandle != null) {
397-
imageHandle.parentNode.removeChild(imageHandle);
397+
//imageHandle.parentNode.removeChild(imageHandle);
398+
OS.destroyHandle(imageHandle);
398399
this.imageHandle = null;
399400
}
400401
}

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/internal/browser/OS.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,38 @@ public static void destroyHandle(Object handle) {
113113
} catch (Error e) {
114114
}
115115
}
116+
if (isIE) {
117+
initGC();
118+
gcContainer.appendChild(el);
119+
el = null;
120+
gcContainer.innerHTML = "";
121+
}
116122
}
117123

118124
public static void clearChildren(Object handle) {
119125
if (handle == null || ((Element) handle).nodeType != 1) {
120126
return ;
121127
}
128+
initGC();
122129
Element el = (Element) handle;
123130
for (int i = el.childNodes.length - 1; i >= 0; i--) {
124-
el.removeChild(el.childNodes[i]);
131+
Element child = el.childNodes[i];
132+
el.removeChild(child);
133+
if (isIE) {
134+
gcContainer.appendChild(child);
135+
child = null;
136+
}
137+
}
138+
if (isIE) {
139+
gcContainer.innerHTML = "";
125140
}
126141
}
127142

128143
public static void deepClearChildren(Object handle) {
129144
if (handle == null) {
130145
return ;
131146
}
147+
initGC();
132148
Element el = (Element) handle;
133149
for (int i = el.childNodes.length - 1; i >= 0; i--) {
134150
Element child = el.childNodes[i];
@@ -137,8 +153,15 @@ public static void deepClearChildren(Object handle) {
137153
destroyHandle(child);
138154
} else {
139155
el.removeChild(child);
156+
if (isIE) {
157+
gcContainer.appendChild(child);
158+
child = null;
159+
}
140160
}
141161
}
162+
if (isIE) {
163+
gcContainer.innerHTML = "";
164+
}
142165
}
143166

144167
public static void SetWindowPos(Object handle, int x, int y, int w, int h, int flags) {
@@ -148,6 +171,7 @@ public static void SetWindowPos(Object handle, int x, int y, int w, int h, int f
148171
//Element el = (Element) handle;
149172
}
150173

174+
private static Element gcContainer;
151175
private static Element invisibleContainer;
152176
private static Object containers;
153177
private static Element lineContainer;
@@ -183,6 +207,16 @@ private static void init() {
183207
blockContainer = el;
184208
}
185209
}
210+
private static void initGC() {
211+
if (isIE) {
212+
if (gcContainer == null) {
213+
Element gc = document.createElement("DIV");
214+
gc.style.display = "none";
215+
document.body.appendChild(gc);
216+
gcContainer = gc;
217+
}
218+
}
219+
}
186220

187221
public static void dispose() {
188222
if (blockContainer != null) {
@@ -206,7 +240,10 @@ public static void dispose() {
206240
* c[p] = null;
207241
* } catch (e) {}
208242
* }
209-
*/ {}
243+
*/ { c.toString(); }
244+
}
245+
if (gcContainer != null) {
246+
gcContainer.parentNode.removeChild(gcContainer);
210247
}
211248
}
212249

@@ -225,7 +262,8 @@ private static void checkScrollBar() {
225262
document.body.appendChild(el);
226263
wScrollBar = el.offsetWidth - el.clientWidth;
227264
hScrollBar = el.offsetHeight - el.clientHeight;
228-
document.body.removeChild(el);
265+
//document.body.removeChild(el);
266+
destroyHandle(el);
229267
}
230268

231269
public static int getScrollBarWidth() {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/internal/dnd/DragAndDrop.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public void unbind() {
5151
Clazz.removeEvent(element, "mouseover", hDND);
5252
hDND = null;
5353
}
54+
element = null;
5455
}
5556
public boolean checkDraggable(HTMLEventWrapper e) {
5657
for (int i = 0; i < this.listeners.length; i++) {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/internal/dnd/SashDND.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
package org.eclipse.swt.internal.dnd;
1313

1414
import org.eclipse.swt.graphics.Point;
15+
import org.eclipse.swt.internal.browser.OS;
1516
import org.eclipse.swt.internal.xhtml.CSSStyle;
1617
import org.eclipse.swt.internal.xhtml.Clazz;
1718
import org.eclipse.swt.internal.xhtml.Element;
@@ -91,9 +92,11 @@ protected void clean() {
9192
thumb.style.display = "none";
9293
document.body.style.cursor = "auto";
9394
Clazz.removeEvent(thumb, "selectstart", DNDUtils.onselectstart);
94-
thumb.parentNode.removeChild(thumb);
95+
//thumb.parentNode.removeChild(thumb);
96+
OS.destroyHandle(thumb);
9597
if (overFrameHandle != null) {
96-
document.body.removeChild(overFrameHandle);
98+
//document.body.removeChild(overFrameHandle);
99+
OS.destroyHandle(overFrameHandle);
97100
overFrameHandle = null;
98101
}
99102
}

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/internal/dnd/ShellFrameDND.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,17 @@ private void clean() {
342342
this.resize = null;
343343

344344
if (overFrameHandle != null) {
345-
document.body.removeChild(overFrameHandle);
345+
//document.body.removeChild(overFrameHandle);
346+
OS.destroyHandle(overFrameHandle);
346347
overFrameHandle = null;
347348
}
348349
};
349350

350351
public void dispose() {
351352
clean();
352353
if (this.frame != null) {
353-
this.frame.parentNode.removeChild(this.frame);
354+
//this.frame.parentNode.removeChild(this.frame);
355+
OS.destroyHandle(this.frame);
354356
this.frame = null;
355357
}
356358
}

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/internal/dnd/TableColumnDND.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
package org.eclipse.swt.internal.dnd;
1313

1414
import org.eclipse.swt.graphics.Point;
15+
import org.eclipse.swt.internal.browser.OS;
1516
import org.eclipse.swt.internal.xhtml.CSSStyle;
1617
import org.eclipse.swt.internal.xhtml.Clazz;
1718
import org.eclipse.swt.internal.xhtml.Element;
@@ -66,7 +67,8 @@ protected void clean() {
6667
thumb.style.display = "none";
6768
Clazz.removeEvent(thumb, "selectstart", DNDUtils.onselectstart);
6869
document.body.style.cursor = "auto";
69-
thumb.parentNode.removeChild(thumb);
70+
//thumb.parentNode.removeChild(thumb);
71+
OS.destroyHandle(thumb);
7072
}
7173

7274
protected Point currentLocation(DragEvent e) {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Control.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,7 @@ protected void releaseChild () {
16991699

17001700
protected void releaseHandle () {
17011701
if (hOperaMouseUp != null || hControlMenuDetect != null) {
1702+
/* FIXME */
17021703
Element el = null;
17031704
if (this instanceof Composite) {
17041705
el = ((Composite) this).containerHandle();
@@ -1715,10 +1716,6 @@ protected void releaseHandle () {
17151716
}
17161717
}
17171718
super.releaseHandle ();
1718-
if (handle != null) {
1719-
OS.destroyHandle(handle);
1720-
handle = null;
1721-
}
17221719
}
17231720

17241721
void releaseWidget () {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/CoolBar.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,8 @@ void destroyItem (CoolItem item) {
755755
error (SWT.ERROR_ITEM_NOT_REMOVED);
756756
}
757757
*/
758-
handle.removeChild(items[index].handle);
758+
//handle.removeChild(items[index].handle);
759+
OS.destroyHandle(items[index].handle);
759760

760761
items [item.id] = null;
761762
item.id = -1;

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Group.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,10 @@ public void setText (String string) {
473473
OS.updateCSSClass(borderFrame, "group-no-title-text", string.length() == 0);
474474
if (string.length() != 0) {
475475
if (!string.equals(groupText)) {
476-
for (int i = titleText.childNodes.length - 1; i >= 0; i--) {
477-
titleText.removeChild(titleText.childNodes[i]);
478-
}
476+
// for (int i = titleText.childNodes.length - 1; i >= 0; i--) {
477+
// titleText.removeChild(titleText.childNodes[i]);
478+
// }
479+
OS.clearChildren(titleText);
479480
titleText.appendChild(document.createTextNode(string));
480481
textWidth = OS.getContainerWidth(titleText);
481482
if (textWidth == 0) {

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Label.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,13 @@ public void setText (String string) {
602602
/*
603603
* Calling setText twice should not make two text node for the handle.
604604
*/
605-
Element[] children = handle.childNodes;
606-
if(children != null){
607-
for(int i = 0; i < children.length; i++){
608-
handle.removeChild(children[i]);
609-
}
610-
}
605+
// Element[] children = handle.childNodes;
606+
// if(children != null){
607+
// for(int i = 0; i < children.length; i++){
608+
// handle.removeChild(children[i]);
609+
// }
610+
// }
611+
OS.clearChildren(handle);
611612
OS.insertText(handle, text);
612613
/*
613614
int newBits = OS.GetWindowLong (handle, OS.GWL_STYLE), oldBits = newBits;

0 commit comments

Comments
 (0)