Skip to content

Commit ebb694a

Browse files
author
zhourenjian
committed
Change on* event callbacks into addEvent/removeEvent to avoid IE memory leak.
1 parent 0f46970 commit ebb694a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1518
-383
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ private OS() {
5050

5151
public static boolean isChrome = false;
5252

53+
public static boolean isChrome10 = false; // Chrome 1.0 or Chrome 0.*
54+
55+
public static boolean isChrome20 = false;
56+
57+
public static boolean isChrome30 = false;
58+
5359

5460
/* Record Caps Lock status */
5561
public static boolean isCapsLockOn = false;
@@ -63,6 +69,9 @@ private OS() {
6369
var isKHTML = (dav.indexOf("Konqueror") >= 0)||(dav.indexOf("Safari") >= 0);
6470
os.isSafari = dav.indexOf("Safari") >= 0;
6571
os.isChrome = dav.indexOf("Chrome") >= 0;
72+
os.isChrome10 = dav.indexOf("Chrome/1.") >= 0 || dav.indexOf("Chrome/0.") >= 0;
73+
os.isChrome20 = dav.indexOf("Chrome/2.") >= 0;
74+
os.isChrome30 = dav.indexOf("Chrome/3.") >= 0;
6675
var geckoPos = dua.indexOf("Gecko");
6776
os.isMozilla = (geckoPos >= 0)&&(!isKHTML);
6877
os.isFirefox = os.isMozilla && dua.indexOf ("Firefox") != -1;
@@ -175,6 +184,31 @@ private static void init() {
175184
}
176185
}
177186

187+
public static void dispose() {
188+
if (blockContainer != null) {
189+
destroyHandle(blockContainer);
190+
blockContainer = null;
191+
}
192+
if (lineContainer != null) {
193+
destroyHandle(lineContainer);
194+
lineContainer = null;
195+
}
196+
if (invisibleContainer != null) {
197+
destroyHandle(invisibleContainer);
198+
invisibleContainer = null;
199+
}
200+
if (containers != null) {
201+
Object c = containers;
202+
/**
203+
* @j2sNative
204+
* for (var p in c) {
205+
* try {
206+
* c[p] = null;
207+
* } catch (e) {}
208+
* }
209+
*/ {}
210+
}
211+
}
178212

179213
private static int wScrollBar = -1;
180214
private static int hScrollBar = -1;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
package org.eclipse.swt.internal.dnd;
1313

14+
import org.eclipse.swt.internal.xhtml.Clazz;
1415
import org.eclipse.swt.internal.xhtml.HTMLEvent;
1516
import org.eclipse.swt.internal.xhtml.document;
1617

@@ -98,7 +99,9 @@ public static boolean onmousedown(Object e, DragAndDrop oThis) {
9899
return true;
99100
}
100101
document.onselectstart = DNDUtils.onselectstart;
101-
evt.target.onselectstart = DNDUtils.onselectstart;
102+
//evt.target.onselectstart = DNDUtils.onselectstart;
103+
Clazz.addEvent(evt.target, "selectstart", DNDUtils.onselectstart);
104+
102105
oThis.startX = evt.x;
103106
oThis.startY = evt.y;
104107
document.onmousemove = DNDUtils.bindFunctionWith (DNDUtils.onmousemove, oThis);

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
package org.eclipse.swt.internal.dnd;
1313

14+
import org.eclipse.swt.internal.xhtml.Clazz;
1415
import org.eclipse.swt.internal.xhtml.Element;
1516
import org.eclipse.swt.internal.xhtml.document;
1617

@@ -25,6 +26,7 @@ public class DragAndDrop {
2526
int startX = 0;
2627
int startY = 0;
2728
private DragListener[] listeners = new DragListener[0];
29+
private Object hDND;
2830

2931
protected void reset() {
3032
status = 0;
@@ -33,13 +35,22 @@ protected void reset() {
3335
document.onselectstart = null;
3436
document.onkeyup = null;
3537
if (this.element != null) {
36-
this.element.onselectstart = null;
38+
//this.element.onselectstart = null;
39+
Clazz.removeEvent(element, "selectstart", DNDUtils.onselectstart);
3740
}
3841
}
3942
public void bind(Element el) {
4043
this.element = el;
41-
el.onmousedown = DNDUtils.bindFunctionWith (DNDUtils.onmousedown, this);
42-
//el.onmouseup = DNDUtils.bindFunctionWith (DNDUtils.onmouseup, this);
44+
hDND = DNDUtils.bindFunctionWith (DNDUtils.onmousedown, this);
45+
// el.onmousedown = ;
46+
Clazz.addEvent(el, "mousedown", hDND);
47+
//el.onmouseup = DNDUtils.bindFunctionWith (DNDUtils.onmouseup, this);
48+
}
49+
public void unbind() {
50+
if (hDND != null) {
51+
Clazz.removeEvent(element, "mouseover", hDND);
52+
hDND = null;
53+
}
4354
}
4455
public boolean checkDraggable(HTMLEventWrapper e) {
4556
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import org.eclipse.swt.graphics.Point;
1515
import org.eclipse.swt.internal.xhtml.CSSStyle;
16+
import org.eclipse.swt.internal.xhtml.Clazz;
1617
import org.eclipse.swt.internal.xhtml.Element;
1718
import org.eclipse.swt.internal.xhtml.document;
1819
import org.eclipse.swt.internal.xhtml.window;
@@ -44,7 +45,8 @@ public boolean dragBegan(DragEvent e) {
4445
thumb.style.top = e.sourceElement.style.top;
4546
thumb.style.width = e.sourceElement.style.width;
4647
thumb.style.height = e.sourceElement.style.height;
47-
thumb.onselectstart = DNDUtils.onselectstart;
48+
//thumb.onselectstart = DNDUtils.onselectstart;
49+
Clazz.addEvent(thumb, "selectstart", DNDUtils.onselectstart);
4850
if (e.sourceElement.nextSibling != null) {
4951
e.sourceElement.parentNode.insertBefore (thumb,
5052
e.sourceElement.nextSibling);
@@ -88,6 +90,7 @@ public boolean dragEnded(DragEvent e) {
8890
protected void clean() {
8991
thumb.style.display = "none";
9092
document.body.style.cursor = "auto";
93+
Clazz.removeEvent(thumb, "selectstart", DNDUtils.onselectstart);
9194
thumb.parentNode.removeChild(thumb);
9295
if (overFrameHandle != null) {
9396
document.body.removeChild(overFrameHandle);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import org.eclipse.swt.graphics.Point;
1515
import org.eclipse.swt.internal.xhtml.CSSStyle;
16+
import org.eclipse.swt.internal.xhtml.Clazz;
1617
import org.eclipse.swt.internal.xhtml.Element;
1718
import org.eclipse.swt.internal.xhtml.document;
1819

@@ -36,7 +37,8 @@ public boolean dragBegan(DragEvent e) {
3637
thumb.style.top = e.sourceElement.style.top;
3738
thumb.style.width = e.sourceElement.style.width;
3839
thumb.style.height = e.sourceElement.style.height;
39-
thumb.onselectstart = DNDUtils.onselectstart;
40+
//thumb.onselectstart = DNDUtils.onselectstart;
41+
Clazz.addEvent(thumb, "selectstart", DNDUtils.onselectstart);
4042
if (e.sourceElement.nextSibling != null) {
4143
e.sourceElement.parentNode.insertBefore (thumb,
4244
e.sourceElement.nextSibling);
@@ -62,6 +64,7 @@ public boolean dragEnded(DragEvent e) {
6264

6365
protected void clean() {
6466
thumb.style.display = "none";
67+
Clazz.removeEvent(thumb, "selectstart", DNDUtils.onselectstart);
6568
document.body.style.cursor = "auto";
6669
thumb.parentNode.removeChild(thumb);
6770
}

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/internal/xhtml/Clazz.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ public class Clazz {
44

55
public native static Runnable makeFunction(Runnable runnable);
66

7+
public native static void addEvent(Object element, String type, Object handler);
8+
9+
public native static void removeEvent(Object element, String type, Object handler);
10+
711
}

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/package.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,22 @@ if (!isDebugging) {
532532
"$.FormLayout"
533533
]);
534534

535+
ClazzLoader.jarClasspath (path + "dnd.z.js", [
536+
"$wt.dnd.DND",
537+
"$.DNDEvent",
538+
"$.DNDListener",
539+
"$.DragSourceListener",
540+
"$.DragSourceAdapter",
541+
"$.DragSourceEvent",
542+
"$.DropTargetListener",
543+
"$.DropTargetAdapter",
544+
"$.DropTargetEvent",
545+
"$.TransferData",
546+
"$.Transfer",
547+
"$.DragSource",
548+
"$.DropTarget"
549+
]);
550+
535551
/*
536552
ClazzLoader.jarClasspath (path + "events.js", [
537553
"java.util.AbstractList",

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

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.eclipse.swt.internal.browser.OS;
2525
import org.eclipse.swt.internal.dnd.HTMLEventWrapper;
2626
import org.eclipse.swt.internal.xhtml.CSSStyle;
27+
import org.eclipse.swt.internal.xhtml.Clazz;
2728
import org.eclipse.swt.internal.xhtml.Element;
2829
import org.eclipse.swt.internal.xhtml.HTMLEvent;
2930
import org.eclipse.swt.internal.xhtml.document;
@@ -68,6 +69,9 @@ public class Button extends Control {
6869
Element btnText;
6970
Element btnHandle;
7071

72+
private Object hSelectionHandler;
73+
private Object hSelectionKeyDown;
74+
7175
/*
7276
static final int ButtonProc;
7377
static final TCHAR ButtonClass = new TCHAR (0,"BUTTON", true);
@@ -509,6 +513,8 @@ void createHandle() {
509513
}
510514
}
511515
}
516+
// Why there are CSS class "button-hover"?
517+
/*
512518
btnHandle.onmouseover = new RunnableCompatibility() {
513519
public void run() {
514520
String cssName = " button-hover";
@@ -527,6 +533,8 @@ public void run() {
527533
}
528534
}
529535
};
536+
*/
537+
530538
//bindHandle();
531539
hookSelection();
532540

@@ -722,11 +730,44 @@ boolean mnemonicMatch (char key) {
722730
* @see org.eclipse.swt.widgets.Control#releaseHandle()
723731
*/
724732
protected void releaseHandle() {
733+
if (hSelectionKeyDown != null) {
734+
Clazz.removeEvent(handle, "keydown", hSelectionKeyDown);
735+
hSelectionKeyDown = null;
736+
}
737+
if (hSelectionHandler != null) {
738+
if ((style & (SWT.RADIO | SWT.CHECK)) != 0) {
739+
Clazz.removeEvent(btnHandle, "click", hSelectionHandler);
740+
Clazz.removeEvent(btnText, "click", hSelectionHandler);
741+
Clazz.removeEvent(btnText, "dblclick", hSelectionHandler);
742+
} else {
743+
Clazz.removeEvent(handle, "click", hSelectionHandler);
744+
Clazz.removeEvent(handle, "dblclick", hSelectionHandler);
745+
}
746+
hSelectionHandler = null;
747+
}
725748
if (btnText != null) {
749+
if (hMouseEnter != null) {
750+
Clazz.removeEvent(btnText, "mouseover", hMouseEnter);
751+
}
752+
if (hMouseExit != null) {
753+
Clazz.removeEvent(btnText, "mouseout", hMouseExit);
754+
}
755+
if (hMouseMove != null) {
756+
Clazz.removeEvent(btnText, "mousemove", hMouseMove);
757+
}
726758
OS.destroyHandle(btnText);
727759
btnText = null;
728760
}
729761
if (btnHandle != null) {
762+
if (hMouseEnter != null) {
763+
Clazz.removeEvent(btnHandle, "mouseover", hMouseEnter);
764+
}
765+
if (hMouseExit != null) {
766+
Clazz.removeEvent(btnHandle, "mouseout", hMouseExit);
767+
}
768+
if (hMouseMove != null) {
769+
Clazz.removeEvent(btnHandle, "mousemove", hMouseMove);
770+
}
730771
OS.destroyHandle(btnHandle);
731772
btnHandle = null;
732773
}
@@ -1354,7 +1395,10 @@ private void updateArrowStyle() {
13541395
}
13551396

13561397
void hookSelection() {
1357-
RunnableCompatibility eventHandler = new RunnableCompatibility() {
1398+
if (hSelectionHandler != null) {
1399+
return;
1400+
}
1401+
hSelectionHandler = new RunnableCompatibility() {
13581402
public void run() {
13591403
if (!isEnabled()) {
13601404
toReturn(false);
@@ -1399,12 +1443,18 @@ public void run() {
13991443
*/
14001444
}
14011445
};
1402-
handle.onclick = handle.ondblclick = eventHandler;
1446+
//handle.onclick = handle.ondblclick = hSelectionHandler;
14031447
if ((style & (SWT.RADIO | SWT.CHECK)) != 0) {
1404-
handle.onclick = handle.ondblclick = null;
1405-
btnHandle.onclick = btnText.onclick = btnText.ondblclick = eventHandler;
1448+
//handle.onclick = handle.ondblclick = null;
1449+
//btnHandle.onclick = btnText.onclick = btnText.ondblclick = hSelectionHandler;
1450+
Clazz.addEvent(btnHandle, "click", hSelectionHandler);
1451+
Clazz.addEvent(btnText, "click", hSelectionHandler);
1452+
Clazz.addEvent(btnText, "dblclick", hSelectionHandler);
1453+
} else {
1454+
Clazz.addEvent(handle, "click", hSelectionHandler);
1455+
Clazz.addEvent(handle, "dblclick", hSelectionHandler);
14061456
}
1407-
handle.onkeydown = new RunnableCompatibility() {
1457+
hSelectionKeyDown = new RunnableCompatibility() {
14081458
public void run() {
14091459
HTMLEvent e = (HTMLEvent) getEvent();
14101460
if(e.keyCode == 32 || e.keyCode == 13){
@@ -1413,21 +1463,29 @@ public void run() {
14131463
toReturn(true);
14141464
}
14151465
};
1466+
//handle.onkeydown = ...
1467+
Clazz.addEvent(handle, "keydown", hSelectionKeyDown);
14161468
}
14171469

14181470
void hookMouseEnter() {
14191471
super.hookMouseEnter();
1420-
btnHandle.onmouseover = btnText.onmouseover = handle.onmouseover;
1472+
Clazz.addEvent(btnHandle, "mouseover", hMouseEnter);
1473+
Clazz.addEvent(btnText, "mouseover", hMouseEnter);
1474+
//btnHandle.onmouseover = btnText.onmouseover = handle.onmouseover;
14211475
}
14221476

14231477
void hookMouseExit() {
14241478
super.hookMouseExit();
1425-
btnHandle.onmouseout = btnText.onmouseout = handle.onmouseout;
1479+
Clazz.addEvent(btnHandle, "mouseout", hMouseExit);
1480+
Clazz.addEvent(btnText, "mouseout", hMouseExit);
1481+
//btnHandle.onmouseout = btnText.onmouseout = handle.onmouseout;
14261482
}
14271483

14281484
void hookMouseMove() {
14291485
super.hookMouseMove();
1430-
btnHandle.onmousemove = btnText.onmousemove = handle.onmousemove;
1486+
Clazz.addEvent(btnHandle, "mousemove", hMouseMove);
1487+
Clazz.addEvent(btnText, "mousemove", hMouseMove);
1488+
//btnHandle.onmousemove = btnText.onmousemove = handle.onmousemove;
14311489
}
14321490

14331491
/*

0 commit comments

Comments
 (0)