From 9bf0344d9a0ed330b6188a45df66a3ac777afb00 Mon Sep 17 00:00:00 2001 From: Tim Buschtoens Date: Wed, 16 Dec 2020 15:53:52 +0100 Subject: [PATCH] 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 --- src/tabris/CanvasContext.js | 4 +++- test/tabris/CanvasContext.test.js | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/tabris/CanvasContext.js b/src/tabris/CanvasContext.js index fbea57b91..a7f61934d 100644 --- a/src/tabris/CanvasContext.js +++ b/src/tabris/CanvasContext.js @@ -197,10 +197,12 @@ defineMethod('drawImage', 3, /** @this {CanvasContext} */ function(image, x1, y1 CanvasContext.getContext = function(canvas, width, height) { if (!canvas._gc) { + const gc = new GC({parent: canvas}); + canvas.on('dispose', () => gc.dispose()); Object.defineProperty(canvas, '_gc', { enumerable: false, writable: false, - value: new GC({parent: canvas}) + value: gc }); } if (!canvas._ctx) { diff --git a/test/tabris/CanvasContext.test.js b/test/tabris/CanvasContext.test.js index 58eb5072e..86ee3daa7 100644 --- a/test/tabris/CanvasContext.test.js +++ b/test/tabris/CanvasContext.test.js @@ -92,13 +92,28 @@ describe('CanvasContext', function() { expect(createCalls[0].properties.parent).to.equal(canvas.cid); }); + it('disposes native GC when parent disposes', function() { + canvas.getContext('2d', 100, 200); + const id = client.calls({op: 'create', type: 'tabris.GC'})[0].id; + tabris.flush(); + + canvas.dispose(); + + console.log(client.calls()); + + const disposeCalls = client.calls({op: 'destroy'}); + expect(disposeCalls.length).to.equal(2); + expect(disposeCalls[0].id).to.equal(id); + expect(disposeCalls[1].id).to.equal(canvas.cid); + }); + it('creates and returns graphics context', function() { ctx = canvas.getContext('2d', 100, 200); expect(ctx).to.be.an.instanceof(CanvasContext); }); - it('returns same instance everytime', function() { + it('returns same instance every time', function() { const ctx1 = canvas.getContext('2d', 100, 200); const ctx2 = canvas.getContext('2d', 100, 200);