Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(sdk): re-loading a spine entity frees the old instance and skeleton

esenginebae6c452f3f1
diff --git a/sdk/src/spine/ModuleBackend.ts b/sdk/src/spine/ModuleBackend.ts
index 4261e35a0..abd782068 100644
--- a/sdk/src/spine/ModuleBackend.ts
+++ b/sdk/src/spine/ModuleBackend.ts
@@ -50,6 +50,11 @@ export class ModuleBackend {
isBinary: boolean,
assetKey?: string,
): boolean {
+ // Re-loading a live entity (rapid editor ref edits, runtime asset swap)
+ // must free the old instance + skeleton refcount first, or replacing its
+ // EntityInfo below leaks the native instance and pins the shared skeleton.
+ if (this.entities_.has(entity)) this.removeEntity(entity);
+
let skelHandle: number;
const shared = assetKey !== undefined ? this.skeletons_.get(assetKey) : undefined;
if (shared) {
diff --git a/sdk/tests/spine-dedup.test.ts b/sdk/tests/spine-dedup.test.ts
index b81e45659..7be3a8341 100644
--- a/sdk/tests/spine-dedup.test.ts
+++ b/sdk/tests/spine-dedup.test.ts
@@ -54,6 +54,31 @@ describe('ModuleBackend skeleton dedup (S4-A)', () => {
expect(c.unloadSkeleton).toHaveBeenCalledTimes(1); // now released
});
+ it('re-loading a live entity onto a different skeleton frees the old instance + skeleton', () => {
+ const c = makeController();
+ const b = new ModuleBackend(c as never);
+ load(b, 1, 'hero');
+ expect(c.createInstance).toHaveBeenCalledTimes(1);
+
+ load(b, 1, 'villain'); // asset swap on the SAME entity
+ expect(c.destroyInstance).toHaveBeenCalledTimes(1); // old 'hero' instance freed
+ expect(c.unloadSkeleton).toHaveBeenCalledTimes(1); // 'hero' refcount hit 0 → unloaded
+ expect(c.createInstance).toHaveBeenCalledTimes(2); // fresh 'villain' instance
+ expect(c.loadSkeleton).toHaveBeenCalledTimes(2);
+ });
+
+ it('re-loading the same shared skeleton keeps it loaded (net-zero refcount), swaps the instance', () => {
+ const c = makeController();
+ const b = new ModuleBackend(c as never);
+ load(b, 1, 'hero');
+ load(b, 2, 'hero');
+ load(b, 1, 'hero'); // re-load entity 1 onto the same shared skeleton
+ expect(c.destroyInstance).toHaveBeenCalledTimes(1); // entity 1's old instance
+ expect(c.unloadSkeleton).not.toHaveBeenCalled(); // entity 2 still holds 'hero'
+ expect(c.createInstance).toHaveBeenCalledTimes(3);
+ expect(c.loadSkeleton).toHaveBeenCalledTimes(1); // still one skeletonData
+ });
+
it('loads a fresh skeleton per entity when no asset key is given (legacy)', () => {
const c = makeController();
const b = new ModuleBackend(c as never);