Skip to content

Commit 9bf0344

Browse files
committed
Dispose GC with Canvas
CanvasContext is wrapping a "GC" NativeObject that needs to be disposed. While the Canvas is set as the "parent" of the GC, the usual mechanism that automatically disposes children of a widget is not in effect here, so the GC needs to be disposed explicitly. Fix #2130 Change-Id: Ic71e7b99a5160d37c636cfaffc8d371691ee849f
1 parent 1ab0b14 commit 9bf0344

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/tabris/CanvasContext.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,12 @@ defineMethod('drawImage', 3, /** @this {CanvasContext} */ function(image, x1, y1
197197

198198
CanvasContext.getContext = function(canvas, width, height) {
199199
if (!canvas._gc) {
200+
const gc = new GC({parent: canvas});
201+
canvas.on('dispose', () => gc.dispose());
200202
Object.defineProperty(canvas, '_gc', {
201203
enumerable: false,
202204
writable: false,
203-
value: new GC({parent: canvas})
205+
value: gc
204206
});
205207
}
206208
if (!canvas._ctx) {

test/tabris/CanvasContext.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,28 @@ describe('CanvasContext', function() {
9292
expect(createCalls[0].properties.parent).to.equal(canvas.cid);
9393
});
9494

95+
it('disposes native GC when parent disposes', function() {
96+
canvas.getContext('2d', 100, 200);
97+
const id = client.calls({op: 'create', type: 'tabris.GC'})[0].id;
98+
tabris.flush();
99+
100+
canvas.dispose();
101+
102+
console.log(client.calls());
103+
104+
const disposeCalls = client.calls({op: 'destroy'});
105+
expect(disposeCalls.length).to.equal(2);
106+
expect(disposeCalls[0].id).to.equal(id);
107+
expect(disposeCalls[1].id).to.equal(canvas.cid);
108+
});
109+
95110
it('creates and returns graphics context', function() {
96111
ctx = canvas.getContext('2d', 100, 200);
97112

98113
expect(ctx).to.be.an.instanceof(CanvasContext);
99114
});
100115

101-
it('returns same instance everytime', function() {
116+
it('returns same instance every time', function() {
102117
const ctx1 = canvas.getContext('2d', 100, 200);
103118

104119
const ctx2 = canvas.getContext('2d', 100, 200);

0 commit comments

Comments
 (0)