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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(sdk): release GL textures on upload failure + drop the createTexture()! assert
esenginef0ec1de8a706
diff --git a/sdk/src/asset/compressed.ts b/sdk/src/asset/compressed.tsindex 6a4359f42..8ac65dfd7 100644--- a/sdk/src/asset/compressed.ts+++ b/sdk/src/asset/compressed.ts@@ -167,9 +167,15 @@ export function uploadCompressedTexture(if (internalFormat == null) throw new Error(`compressed upload: no GL internalformat for ${fmt}`);const texture = gl.createTexture();if (!texture) throw new Error('compressed upload: gl.createTexture failed');- gl.bindTexture(gl.TEXTURE_2D, texture);- gl.compressedTexImage2D(gl.TEXTURE_2D, 0, internalFormat, t.width, t.height, 0, t.data);- applyParams(gl, opts);+ try {+ gl.bindTexture(gl.TEXTURE_2D, texture);+ gl.compressedTexImage2D(gl.TEXTURE_2D, 0, internalFormat, t.width, t.height, 0, t.data);+ applyParams(gl, opts);+ } catch (err) {+ // Release the GL texture if the upload throws — don't leak it.+ gl.deleteTexture(texture);+ throw err;+ }return { handle: registerGlTexture(module, texture, t.width, t.height), width: t.width, height: t.height };}@@ -179,9 +185,15 @@ export function uploadRgbaTexture(): UploadedTexture {const texture = gl.createTexture();if (!texture) throw new Error('rgba upload: gl.createTexture failed');- gl.bindTexture(gl.TEXTURE_2D, texture);- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, r.width, r.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, r.data);- applyParams(gl, opts);+ try {+ gl.bindTexture(gl.TEXTURE_2D, texture);+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, r.width, r.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, r.data);+ applyParams(gl, opts);+ } catch (err) {+ // Release the GL texture if the upload throws — don't leak it.+ gl.deleteTexture(texture);+ throw err;+ }return { handle: registerGlTexture(module, texture, r.width, r.height), width: r.width, height: r.height };}diff --git a/sdk/src/asset/loaders/TextureLoader.ts b/sdk/src/asset/loaders/TextureLoader.tsindex 31a7898b9..ccd14d805 100644--- a/sdk/src/asset/loaders/TextureLoader.ts+++ b/sdk/src/asset/loaders/TextureLoader.ts@@ -185,19 +185,31 @@ export class TextureLoader implements AssetLoader<TextureResult> {wrap === 'mirror' ? gl.MIRRORED_REPEAT :gl.REPEAT;- const texture = gl.createTexture()!;- gl.bindTexture(gl.TEXTURE_2D, texture);- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flip ? 1 : 0);- gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img as any);- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, glMinFilter);- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, glMagFilter);- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, glWrap);- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, glWrap);- if (useMipmaps) {- gl.generateMipmap(gl.TEXTURE_2D);+ // createTexture returns null on a lost context — don't `!`-assert it+ // into the calls below.+ const texture = gl.createTexture();+ if (!texture) {+ throw new Error('TextureLoader: gl.createTexture() returned null (GL context lost?)');+ }+ try {+ gl.bindTexture(gl.TEXTURE_2D, texture);+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flip ? 1 : 0);+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img as any);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, glMinFilter);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, glMagFilter);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, glWrap);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, glWrap);+ if (useMipmaps) {+ gl.generateMipmap(gl.TEXTURE_2D);+ }+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);+ } catch (err) {+ // Upload threw (e.g. context lost mid-call); release the GL texture+ // instead of leaking it.+ gl.deleteTexture(texture);+ throw err;}- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);const glObj = this.module_.GL;const glTextureId = glObj.getNewId(glObj.textures);diff --git a/sdk/tests/texture-upload-leak.test.ts b/sdk/tests/texture-upload-leak.test.tsnew file mode 100644index 000000000..dbbc37996--- /dev/null+++ b/sdk/tests/texture-upload-leak.test.ts@@ -0,0 +1,57 @@+/**+ * @file texture-upload-leak.test.ts+ * @brief A GL texture created during upload must be released if the upload+ * throws (lost context / bad source), and gl.createTexture() returning+ * null must not be `!`-asserted into the upload calls — otherwise each+ * failed upload leaks a GL texture.+ */+import { describe, expect, it, vi } from 'vitest';+import { TextureLoader } from '../src/asset/loaders/TextureLoader';+import { uploadRgbaTexture } from '../src/asset/compressed';++function mockGl(opts?: { createReturnsNull?: boolean; throwOnUpload?: boolean }) {+ const tex = { __tex: true };+ const gl: any = {+ TEXTURE_2D: 0x0de1, RGBA: 0x1908, UNSIGNED_BYTE: 0x1401,+ TEXTURE_MIN_FILTER: 0, TEXTURE_MAG_FILTER: 0, TEXTURE_WRAP_S: 0, TEXTURE_WRAP_T: 0,+ LINEAR: 0x2601, NEAREST: 0x2600, LINEAR_MIPMAP_LINEAR: 0, NEAREST_MIPMAP_NEAREST: 0,+ REPEAT: 0x2901, CLAMP_TO_EDGE: 0x812f, MIRRORED_REPEAT: 0x8370,+ UNPACK_FLIP_Y_WEBGL: 0, UNPACK_PREMULTIPLY_ALPHA_WEBGL: 0,+ createTexture: vi.fn(() => (opts?.createReturnsNull ? null : tex)),+ bindTexture: vi.fn(),+ pixelStorei: vi.fn(),+ texImage2D: vi.fn(() => { if (opts?.throwOnUpload) throw new Error('GL upload failed'); }),+ compressedTexImage2D: vi.fn(() => { if (opts?.throwOnUpload) throw new Error('GL upload failed'); }),+ texParameteri: vi.fn(),+ generateMipmap: vi.fn(),+ deleteTexture: vi.fn(),+ };+ return { gl, tex };+}++describe('TextureLoader.createTextureWebGL2', () => {+ it('throws clearly when gl.createTexture() returns null (no `!` assert)', () => {+ const loader = new TextureLoader({} as any);+ const { gl } = mockGl({ createReturnsNull: true });+ expect(() => (loader as any).createTextureWebGL2(gl, {} as any, 4, 4, true))+ .toThrow(/createTexture\(\) returned null/);+ });++ it('releases the GL texture when the upload throws (no leak)', () => {+ const loader = new TextureLoader({} as any);+ const { gl, tex } = mockGl({ throwOnUpload: true });+ expect(() => (loader as any).createTextureWebGL2(gl, {} as any, 4, 4, true))+ .toThrow(/GL upload failed/);+ expect(gl.deleteTexture).toHaveBeenCalledWith(tex);+ });+});++describe('compressed uploadRgbaTexture', () => {+ it('releases the GL texture when the upload throws (no leak)', () => {+ const { gl, tex } = mockGl({ throwOnUpload: true });+ const r = { width: 4, height: 4, data: new Uint8Array(64) };+ expect(() => uploadRgbaTexture(gl, {} as any, r as any))+ .toThrow(/GL upload failed/);+ expect(gl.deleteTexture).toHaveBeenCalledWith(tex);+ });+});