Skip to content

Commit

Permalink
Dispose GC with Canvas
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tbuschto committed Dec 16, 2020
1 parent 1ab0b14 commit 9bf0344
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/tabris/CanvasContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 16 additions & 1 deletion test/tabris/CanvasContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9bf0344

Please sign in to comment.