Skip to content

Commit

Permalink
call delete methods on cleanup
Browse files Browse the repository at this point in the history
turns out we had two methods that were cleaning the renderer and the listeners but we were not invoking them on cleanup.
this was causing leaks, the largest one being detached dom elements retained.

Diffs=
4933ad6cec call delete methods on cleanup (#8704)

Co-authored-by: hernan <[email protected]>
  • Loading branch information
bodymovin and bodymovin committed Dec 11, 2024
1 parent 3fd3825 commit 0cc2564
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c7c1701511e79979acb7ad863859aa01adbcf3a9
4933ad6cec0cb892a27601aa485e81725f99efaf
19 changes: 18 additions & 1 deletion js/src/rive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ export class StateMachineInput {
this.runtimeInput.fire();
}
}

/**
* Deletes the input
*/
public delete(): void {
this.runtimeInput = null;
}
}

export enum RiveEventType {
Expand Down Expand Up @@ -462,6 +469,10 @@ class StateMachine {
* state machine is no more.
*/
public cleanup() {
this.inputs.forEach((input) => {
input.delete();
});
this.inputs.length = 0;
this.instance.delete();
}
}
Expand Down Expand Up @@ -1489,7 +1500,7 @@ export class Rive {
private loaded = false;

// Reference of an object that handles any observers for the animation
private _observed: ObservedObject | null;
private _observed: ObservedObject | null = null;

/**
* Tracks if a Rive file is loaded; we need this in addition to loaded as some
Expand Down Expand Up @@ -1680,6 +1691,9 @@ export class Rive {
.then((runtime) => {
this.runtime = runtime;

this.removeRiveListeners();
this.deleteRiveRenderer();

// Get the canvas where you want to render the animation and create a renderer
this.renderer = this.runtime.makeRenderer(
this.canvas,
Expand Down Expand Up @@ -1749,6 +1763,7 @@ export class Rive {
public removeRiveListeners(): void {
if (this.eventCleanup) {
this.eventCleanup();
this.eventCleanup = null;
}
}

Expand Down Expand Up @@ -2104,10 +2119,12 @@ export class Rive {
if (this._observed !== null) {
observers.remove(this._observed);
}
this.removeRiveListeners();

this.riveFile?.cleanup();
this.riveFile = null;
this.file = null;
this.deleteRiveRenderer();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/test/initialisation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ test("Rive deletes instances on the cleanup", (done) => {
expect(r.activeArtboard).toBe("MyArtboard");
r.cleanup();
expect(r.activeArtboard).toBe("");
expect(r["renderer"]).not.toBeNull();
expect(r["renderer"]).toBeNull();
done();
},
});
Expand Down

0 comments on commit 0cc2564

Please sign in to comment.