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(scene): four asset types were preloaded on every scene load and never released

esenginea3e19b618048
diff --git a/sdk/src/scene/sceneManager.ts b/sdk/src/scene/sceneManager.ts
index 657243d31..fbcfb06d2 100644
--- a/sdk/src/scene/sceneManager.ts
+++ b/sdk/src/scene/sceneManager.ts
@@ -73,13 +73,14 @@ class SceneInstance {
readonly postProcessBindings = new Map<Entity, PostProcessStack>();
readonly savedEnabled = new Map<Entity, Map<RenderableComponentDef, boolean>>();
readonly systemIds: symbol[] = [];
- loadedTextures: Set<string> | null = null;
+ /**
+ * Every path-keyed asset this scene acquired, by declared type. NOT a fixed
+ * list: it was seven fields, and the four types added since were preloaded
+ * and never released, because adding one meant remembering three places.
+ */
+ readonly loadedByType = new Map<string, Set<string>>();
+ /** Materials are keyed by HANDLE, not path — released through their own door. */
loadedMaterials: Set<number> | null = null;
- loadedFonts: Set<string> | null = null;
- loadedAudio: Set<string> | null = null;
- loadedAnimClips: Set<string> | null = null;
- loadedTilemaps: Set<string> | null = null;
- loadedTimelines: Set<string> | null = null;
status: SceneStatus = 'loading';
constructor(config: SceneConfig) {
@@ -472,21 +473,26 @@ export class SceneManagerState {
): Promise<void> {
if (sceneData) {
const discovered = discoverSceneAssets(sceneData);
- instance.loadedTextures = discovered.byType.get('texture') ?? new Set();
+ instance.loadedByType.clear();
+ // Whatever the scene declared, whatever its type. Materials are
+ // collected as handles below; skeletons belong to the SpineManager.
+ for (const [type, paths] of discovered.byType) {
+ if (type === 'material' || type === 'spine') continue;
+ instance.loadedByType.set(type, new Set(paths));
+ }
+ const bucketFor = (type: string): Set<string> => {
+ let set = instance.loadedByType.get(type);
+ if (!set) { set = new Set(); instance.loadedByType.set(type, set); }
+ return set;
+ };
instance.loadedMaterials = new Set();
- instance.loadedFonts = discovered.byType.get('font') ?? new Set();
- // Fire-and-forget asset types that previously leaked on unload —
- // track what the scene declares so releaseSceneAssets_ can drop
- // them via the matching Assets.release* method.
- instance.loadedAudio = discovered.byType.get('audio') ?? new Set();
- instance.loadedAnimClips = discovered.byType.get('anim-clip') ?? new Set();
- instance.loadedTilemaps = discovered.byType.get('tilemap') ?? new Set();
- instance.loadedTimelines = discovered.byType.get('timeline') ?? new Set();
const collectAssets: LoadedSceneAssets = {
- texturePaths: instance.loadedTextures,
+ // The SAME sets the preloader fills in with what it actually
+ // loaded, so the release side sees acquisitions, not intentions.
+ texturePaths: bucketFor('texture'),
materialHandles: instance.loadedMaterials,
- fontPaths: instance.loadedFonts,
+ fontPaths: bucketFor('font'),
spineKeys: new Set(),
};
@@ -547,9 +553,15 @@ export class SceneManagerState {
? this.app_.getResource(Assets)
: null;
- if (assetsRes && instance.loadedTextures) {
- for (const path of instance.loadedTextures) {
- assetsRes.releaseTexture(path);
+ if (assetsRes) {
+ // Driven by what the scene acquired and the loader registry, not by a
+ // list: a type with a registered loader has a release channel, and a
+ // type without one no-ops. Adding an asset kind touches neither.
+ for (const [type, paths] of instance.loadedByType) {
+ for (const path of paths) {
+ if (type === 'texture') assetsRes.releaseTexture(path);
+ else assetsRes.releaseTyped(type, path);
+ }
}
}
@@ -563,34 +575,8 @@ export class SceneManagerState {
}
}
- if (assetsRes && instance.loadedFonts) {
- for (const path of instance.loadedFonts) {
- assetsRes.releaseFont(path);
- }
- }
-
- if (assetsRes) {
- if (instance.loadedAudio) {
- for (const path of instance.loadedAudio) assetsRes.releaseAudio(path);
- }
- if (instance.loadedAnimClips) {
- for (const path of instance.loadedAnimClips) assetsRes.releaseAnimClip(path);
- }
- if (instance.loadedTilemaps) {
- for (const path of instance.loadedTilemaps) assetsRes.releaseTilemap(path);
- }
- if (instance.loadedTimelines) {
- for (const path of instance.loadedTimelines) assetsRes.releaseTimeline(path);
- }
- }
-
- instance.loadedTextures = null;
+ instance.loadedByType.clear();
instance.loadedMaterials = null;
- instance.loadedFonts = null;
- instance.loadedAudio = null;
- instance.loadedAnimClips = null;
- instance.loadedTilemaps = null;
- instance.loadedTimelines = null;
}
pause(name: string): void {
diff --git a/sdk/tests/scene-asset-release.test.ts b/sdk/tests/scene-asset-release.test.ts
index d248c0dbc..77d8b95cc 100644
--- a/sdk/tests/scene-asset-release.test.ts
+++ b/sdk/tests/scene-asset-release.test.ts
@@ -23,6 +23,14 @@ vi.mock('../src/render/material', () => ({
Material: { release: vi.fn(), createShader: vi.fn() },
defineResource: vi.fn(),
}));
+// Discovery is stubbed so a scene can declare a type without the component
+// registry: the claim under test is that the manager buckets WHATEVER
+// discovery reports, not that discovery finds the right things.
+const discovered = { byType: new Map<string, Set<string>>() };
+vi.mock('../src/asset/discoverAssets', () => ({
+ discoverSceneAssets: vi.fn(() => discovered),
+ getAssetPathsByType: vi.fn(() => new Set()),
+}));
import { SceneManagerState } from '../src/scene/sceneManager';
import { Assets } from '../src/asset';
@@ -57,65 +65,92 @@ function createMockApp(assets?: unknown) {
}
describe('Scene unload releases all tracked asset categories', () => {
- it('calls every category-specific release method on Assets', async () => {
- const releaseLog: Record<string, string[]> = {
- texture: [], font: [], audio: [], animClip: [], timeline: [], tilemap: [],
- };
+ // Shared across the mock module, so a test that seeds it must not leak into
+ // the next one's scene.
+ beforeEach(() => { discovered.byType = new Map(); });
+
+ /**
+ * Every path-keyed type a scene can declare, one path each.
+ *
+ * `tileset`, `statemachine`, `behaviortree` and `animatorcontroller` are the
+ * four this list lacked: each was preloaded, none was tracked, so unload
+ * released nothing. A type with a registered loader belongs here.
+ */
+ const TRACKED: Array<[type: string, path: string]> = [
+ ['texture', 'tex/a.png'],
+ ['font', 'font/main.ttf'],
+ ['audio', 'sfx/boom.wav'],
+ ['anim-clip', 'anim/walk.json'],
+ ['timeline', 'timeline/intro.json'],
+ ['tilemap', 'maps/level1.tmx'],
+ ['tileset', 'maps/tiles.estileset'],
+ ['statemachine', 'ai/guard.esfsm'],
+ ['behaviortree', 'ai/patrol.esbt'],
+ ['animatorcontroller', 'anim/hero.esanimator'],
+ ];
+
+ it('buckets every type discovery reports, including ones added later', async () => {
+ // The original bug lived HERE, not in the release wiring: seven
+ // hard-coded buckets, so a scene declaring a tileset acquired a
+ // reference the manager never recorded and unload could not give back.
+ discovered.byType = new Map(TRACKED.map(([type, path]) => [type, new Set([path])]));
+
+ const app = createMockApp({ releaseTexture: vi.fn(), releaseTyped: vi.fn() });
+ const manager = new SceneManagerState(app as never);
+ manager.register({ name: 'declaring', data: { version: '1.0', name: 'd', entities: [] } });
+ await manager.load('declaring');
+
+ const instance = (manager as unknown as {
+ scenes_: Map<string, { loadedByType: Map<string, Set<string>> }>;
+ }).scenes_.get('declaring')!;
+
+ for (const [type, path] of TRACKED) {
+ expect(
+ [...(instance.loadedByType.get(type) ?? [])],
+ `the scene declared a ${type} and the manager kept no bucket for it`,
+ ).toContain(path);
+ }
+ });
+
+ it('releases every type the scene acquired, whatever its type', async () => {
+ const releasedTextures: string[] = [];
+ const releasedTyped: Array<[string, string]> = [];
const assetsStub = {
- releaseTexture: (r: string) => { releaseLog.texture.push(r); },
- releaseFont: (r: string) => { releaseLog.font.push(r); },
- releaseAudio: (r: string) => { releaseLog.audio.push(r); },
- releaseAnimClip: (r: string) => { releaseLog.animClip.push(r); },
- releaseTimeline: (r: string) => { releaseLog.timeline.push(r); },
- releaseTilemap: (r: string) => { releaseLog.tilemap.push(r); },
+ releaseTexture: (r: string) => { releasedTextures.push(r); },
+ releaseTyped: (type: string, r: string) => { releasedTyped.push([type, r]); },
};
const app = createMockApp(assetsStub);
const manager = new SceneManagerState(app as never);
-
- manager.register({
- name: 'level1',
- data: { version: '1.0', name: 'level1', entities: [] },
- });
+ manager.register({ name: 'level1', data: { version: '1.0', name: 'level1', entities: [] } });
await manager.load('level1');
- // Seed the scene-instance buckets with paths directly. This bypasses
- // asset-field discovery (which needs component registry) and proves
- // the release wiring itself is complete for every category the audit
- // flagged as leaking.
+ // Seeded directly: discovery needs the component registry, and the claim
+ // here is about the release wiring, not about what a scene declares.
const instance = (manager as unknown as {
- scenes_: Map<string, {
- loadedTextures: Set<string>;
- loadedFonts: Set<string>;
- loadedAudio: Set<string>;
- loadedAnimClips: Set<string>;
- loadedTimelines: Set<string>;
- loadedTilemaps: Set<string>;
- loadedMaterials: Set<number>;
- }>;
+ scenes_: Map<string, { loadedByType: Map<string, Set<string>> }>;
}).scenes_.get('level1')!;
- instance.loadedTextures = new Set(['tex/a.png', 'tex/b.png']);
- instance.loadedFonts = new Set(['font/main.ttf']);
- instance.loadedAudio = new Set(['sfx/boom.wav']);
- instance.loadedAnimClips = new Set(['anim/walk.json']);
- instance.loadedTimelines = new Set(['timeline/intro.json']);
- instance.loadedTilemaps = new Set(['maps/level1.tmx']);
+ for (const [type, path] of TRACKED) instance.loadedByType.set(type, new Set([path]));
await manager.unload('level1');
- expect(releaseLog.texture).toEqual(expect.arrayContaining(['tex/a.png', 'tex/b.png']));
- expect(releaseLog.font).toEqual(['font/main.ttf']);
- expect(releaseLog.audio).toEqual(['sfx/boom.wav']);
- expect(releaseLog.animClip).toEqual(['anim/walk.json']);
- expect(releaseLog.timeline).toEqual(['timeline/intro.json']);
- expect(releaseLog.tilemap).toEqual(['maps/level1.tmx']);
+ for (const [type, path] of TRACKED) {
+ if (type === 'texture') {
+ expect(releasedTextures, 'a texture was never released').toContain(path);
+ continue;
+ }
+ expect(
+ releasedTyped,
+ `a ${type} the scene acquired was never released on unload`,
+ ).toContainEqual([type, path]);
+ }
});
it('releases tracked materials through Assets by handle, not a bare Material.release', async () => {
vi.mocked(Material.release).mockClear();
const releasedHandles: number[] = [];
const assetsStub = {
- releaseTexture: vi.fn(), releaseFont: vi.fn(), releaseAudio: vi.fn(),
+ releaseTexture: vi.fn(), releaseTyped: vi.fn(), releaseFont: vi.fn(), releaseAudio: vi.fn(),
releaseAnimClip: vi.fn(), releaseTimeline: vi.fn(), releaseTilemap: vi.fn(),
releaseMaterial: (h: number) => { releasedHandles.push(h); },
};