From d9664cd4beb721bfb440b0f519a2742c01d1efa5 Mon Sep 17 00:00:00 2001 From: bofeng Date: Tue, 10 Dec 2024 11:43:47 +0800 Subject: [PATCH 1/4] Fix memory leaks in Spine caused by not calling freeStoreMemory. --- cocos/spine/lib/spine-core.d.ts | 1 + cocos/spine/skeleton-data.ts | 1 + native/cocos/editor-support/spine-wasm/spine-type-export.cpp | 1 + 3 files changed, 3 insertions(+) diff --git a/cocos/spine/lib/spine-core.d.ts b/cocos/spine/lib/spine-core.d.ts index 6bcf725f17e..a1c3eb44893 100644 --- a/cocos/spine/lib/spine-core.d.ts +++ b/cocos/spine/lib/spine-core.d.ts @@ -1214,6 +1214,7 @@ declare namespace spine { class wasmUtil { static spineWasmInit(): void; static spineWasmDestroy(): void; + static freeStoreMemory(): void; static queryStoreMemory(size: number): number; static querySpineSkeletonDataByUUID(uuid: string): SkeletonData; static createSpineSkeletonDataWithJson(jsonStr: string, atlasText: string): SkeletonData; diff --git a/cocos/spine/skeleton-data.ts b/cocos/spine/skeleton-data.ts index d872fd1ccc2..4be051a4c7b 100644 --- a/cocos/spine/skeleton-data.ts +++ b/cocos/spine/skeleton-data.ts @@ -223,6 +223,7 @@ export class SkeletonData extends Asset { wasmMem.set(rawData); this._skeletonCache = spine.wasmUtil.createSpineSkeletonDataWithBinary(byteSize, this._atlasText); spine.wasmUtil.registerSpineSkeletonDataWithUUID(this._skeletonCache, uuid); + spine.wasmUtil.freeStoreMemory(); } return this._skeletonCache; diff --git a/native/cocos/editor-support/spine-wasm/spine-type-export.cpp b/native/cocos/editor-support/spine-wasm/spine-type-export.cpp index ac34fbc3f01..77e006afbed 100644 --- a/native/cocos/editor-support/spine-wasm/spine-type-export.cpp +++ b/native/cocos/editor-support/spine-wasm/spine-type-export.cpp @@ -1383,6 +1383,7 @@ EMSCRIPTEN_BINDINGS(cocos_spine) { .class_function("spineWasmInit", &SpineWasmUtil::spineWasmInit) .class_function("spineWasmDestroy", &SpineWasmUtil::spineWasmDestroy) .class_function("queryStoreMemory", &SpineWasmUtil::queryStoreMemory) + .class_function("freeStoreMemory", &SpineWasmUtil::freeStoreMemory) .class_function("querySpineSkeletonDataByUUID", &SpineWasmUtil::querySpineSkeletonDataByUUID, allow_raw_pointers()) .class_function("createSpineSkeletonDataWithJson", &SpineWasmUtil::createSpineSkeletonDataWithJson, allow_raw_pointers()) .class_function("createSpineSkeletonDataWithBinary", &SpineWasmUtil::createSpineSkeletonDataWithBinary, allow_raw_pointers()) From ee848be334ec550bf9a29440728e4a823c850ff4 Mon Sep 17 00:00:00 2001 From: bofeng Date: Tue, 10 Dec 2024 13:47:18 +0800 Subject: [PATCH 2/4] refine --- .../cocos/editor-support/spine-wasm/spine-wasm.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/native/cocos/editor-support/spine-wasm/spine-wasm.cpp b/native/cocos/editor-support/spine-wasm/spine-wasm.cpp index cc486db5c5a..29947efe70d 100644 --- a/native/cocos/editor-support/spine-wasm/spine-wasm.cpp +++ b/native/cocos/editor-support/spine-wasm/spine-wasm.cpp @@ -96,16 +96,9 @@ void SpineWasmUtil::destroySpineSkeleton(Skeleton* skeleton) { } uint32_t SpineWasmUtil::queryStoreMemory(uint32_t size) { - if (s_mem) { - if (s_memSize < size) { - delete[] s_mem; - s_mem = new uint8_t[size]; - s_memSize = size; - } - } else { - s_mem = new uint8_t[size]; - s_memSize = size; - } + s_mem = new uint8_t[size]; + s_memSize = size; + return (uint32_t)s_mem; } From d171fc9848b1ef1268af85fd2b8fae589b361951 Mon Sep 17 00:00:00 2001 From: bofeng Date: Wed, 11 Dec 2024 16:34:29 +0800 Subject: [PATCH 3/4] refine --- cocos/spine/skeleton-data.ts | 2 +- native/cocos/editor-support/spine-wasm/spine-type-export.cpp | 2 +- native/cocos/editor-support/spine-wasm/spine-wasm.cpp | 5 +---- native/cocos/editor-support/spine-wasm/spine-wasm.h | 3 +-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/cocos/spine/skeleton-data.ts b/cocos/spine/skeleton-data.ts index 4be051a4c7b..54d51d6f1d1 100644 --- a/cocos/spine/skeleton-data.ts +++ b/cocos/spine/skeleton-data.ts @@ -218,7 +218,7 @@ export class SkeletonData extends Asset { } else { const rawData = new Uint8Array(this._nativeAsset); const byteSize = rawData.length; - const ptr = spine.wasmUtil.queryStoreMemory(byteSize); + const ptr = spine.wasmUtil.createStoreMemory(byteSize); const wasmMem = spine.wasmUtil.wasm.HEAPU8.subarray(ptr, ptr + byteSize); wasmMem.set(rawData); this._skeletonCache = spine.wasmUtil.createSpineSkeletonDataWithBinary(byteSize, this._atlasText); diff --git a/native/cocos/editor-support/spine-wasm/spine-type-export.cpp b/native/cocos/editor-support/spine-wasm/spine-type-export.cpp index 77e006afbed..8c1296674c9 100644 --- a/native/cocos/editor-support/spine-wasm/spine-type-export.cpp +++ b/native/cocos/editor-support/spine-wasm/spine-type-export.cpp @@ -1382,7 +1382,7 @@ EMSCRIPTEN_BINDINGS(cocos_spine) { class_("SpineWasmUtil") .class_function("spineWasmInit", &SpineWasmUtil::spineWasmInit) .class_function("spineWasmDestroy", &SpineWasmUtil::spineWasmDestroy) - .class_function("queryStoreMemory", &SpineWasmUtil::queryStoreMemory) + .class_function("createStoreMemory", &SpineWasmUtil::createStoreMemory) .class_function("freeStoreMemory", &SpineWasmUtil::freeStoreMemory) .class_function("querySpineSkeletonDataByUUID", &SpineWasmUtil::querySpineSkeletonDataByUUID, allow_raw_pointers()) .class_function("createSpineSkeletonDataWithJson", &SpineWasmUtil::createSpineSkeletonDataWithJson, allow_raw_pointers()) diff --git a/native/cocos/editor-support/spine-wasm/spine-wasm.cpp b/native/cocos/editor-support/spine-wasm/spine-wasm.cpp index 29947efe70d..d0bbcc00a14 100644 --- a/native/cocos/editor-support/spine-wasm/spine-wasm.cpp +++ b/native/cocos/editor-support/spine-wasm/spine-wasm.cpp @@ -15,7 +15,6 @@ EventType SpineWasmUtil::s_currentType = EventType_Event; TrackEntry* SpineWasmUtil::s_currentEntry = nullptr; Event* SpineWasmUtil::s_currentEvent = nullptr; uint8_t* SpineWasmUtil::s_mem = nullptr; -uint32_t SpineWasmUtil::s_memSize = 0; void SpineWasmUtil::spineWasmInit() { // LogUtil::Initialize(); @@ -95,9 +94,8 @@ void SpineWasmUtil::destroySpineSkeleton(Skeleton* skeleton) { } } -uint32_t SpineWasmUtil::queryStoreMemory(uint32_t size) { +uint32_t SpineWasmUtil::createStoreMemory(uint32_t size) { s_mem = new uint8_t[size]; - s_memSize = size; return (uint32_t)s_mem; } @@ -107,7 +105,6 @@ void SpineWasmUtil::freeStoreMemory() { delete[] s_mem; s_mem = nullptr; } - s_memSize = 0; } uint32_t SpineWasmUtil::getCurrentListenerID() { diff --git a/native/cocos/editor-support/spine-wasm/spine-wasm.h b/native/cocos/editor-support/spine-wasm/spine-wasm.h index 655b7f743c1..26c775571c7 100644 --- a/native/cocos/editor-support/spine-wasm/spine-wasm.h +++ b/native/cocos/editor-support/spine-wasm/spine-wasm.h @@ -7,7 +7,7 @@ class SpineWasmUtil { public: static void spineWasmInit(); static void spineWasmDestroy(); - static uint32_t queryStoreMemory(uint32_t size); + static uint32_t createStoreMemory(uint32_t size); static void freeStoreMemory(); static spine::SkeletonData* querySpineSkeletonDataByUUID(const spine::String& uuid); @@ -28,5 +28,4 @@ class SpineWasmUtil { static spine::Event* s_currentEvent; static uint8_t* s_mem; - static uint32_t s_memSize; }; From 55be7090ccf9a1f32e15f19888929a9f765a662a Mon Sep 17 00:00:00 2001 From: bofeng Date: Wed, 11 Dec 2024 16:36:08 +0800 Subject: [PATCH 4/4] refine --- cocos/spine/lib/spine-core.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/spine/lib/spine-core.d.ts b/cocos/spine/lib/spine-core.d.ts index a1c3eb44893..3a1fcbc1a24 100644 --- a/cocos/spine/lib/spine-core.d.ts +++ b/cocos/spine/lib/spine-core.d.ts @@ -1215,7 +1215,7 @@ declare namespace spine { static spineWasmInit(): void; static spineWasmDestroy(): void; static freeStoreMemory(): void; - static queryStoreMemory(size: number): number; + static createStoreMemory(size: number): number; static querySpineSkeletonDataByUUID(uuid: string): SkeletonData; static createSpineSkeletonDataWithJson(jsonStr: string, atlasText: string): SkeletonData; static createSpineSkeletonDataWithBinary(byteSize: number, atlasText: string): SkeletonData;