Skip to content

Commit 535a593

Browse files
authored
Fix memory leaks in Spine resource parsing on the web-side. (#65)
* Fix memory leaks in Spine resource parsing on the web-side. * update external version
1 parent 5f4d70d commit 535a593

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

native/cocos/editor-support/spine-wasm/spine-wasm.cpp

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -171,57 +171,60 @@ SkeletonData* SpineWasmUtil::querySpineSkeletonDataByUUID(const String& uuid) {
171171
}
172172

173173
SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithJson(const String& jsonStr, const String& altasStr, const spine::Vector<spine::String>& textureNames, const spine::Vector<spine::String>& textureUUIDs) {
174+
SkeletonData* skeletonData = nullptr;
174175
#if ENABLE_JSON_PARSER
175176
auto* atlas = new Atlas(altasStr.buffer(), altasStr.length(), "", nullptr, false);
176177
if (!atlas) {
177178
return nullptr;
178179
}
179-
AttachmentLoader* attachmentLoader = new AtlasAttachmentLoaderExtension(atlas);
180-
#ifdef CC_SPINE_VERSION_3_8
181-
SkeletonJson json(attachmentLoader);
182-
#else
183-
SkeletonJson json(attachmentLoader, true);
184-
#endif
185-
json.setScale(1.0F);
186-
SkeletonData* skeletonData = json.readSkeletonData(jsonStr.buffer());
187-
auto& errorMsg = json.getError();
188-
if (!errorMsg.isEmpty()) {
189-
logToConsole(errorMsg.buffer(), LOG_LEVEL_WARN);
190-
}
180+
{
181+
AttachmentLoader* attachmentLoader = new AtlasAttachmentLoaderExtension(atlas);
182+
#ifdef CC_SPINE_VERSION_3_8
183+
SkeletonJson json(attachmentLoader);
184+
#else
185+
SkeletonJson json(attachmentLoader, true);
186+
#endif
187+
json.setScale(1.0F);
188+
skeletonData = json.readSkeletonData(jsonStr.buffer());
189+
auto& errorMsg = json.getError();
190+
if (!errorMsg.isEmpty()) {
191+
logToConsole(errorMsg.buffer(), LOG_LEVEL_WARN);
192+
}
191193

192-
saveAttachmentVertices(skeletonData, textureNames, textureUUIDs);
194+
saveAttachmentVertices(skeletonData, textureNames, textureUUIDs);
195+
}
196+
delete atlas;
197+
#endif
193198

194199
return skeletonData;
195-
#else
196-
return nullptr;
197-
#endif
198200
}
199201

200202
SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithBinary(uint32_t byteSize, const String& altasStr, const spine::Vector<spine::String>& textureNames, const spine::Vector<spine::String>& textureUUIDs) {
203+
SkeletonData* skeletonData = nullptr;
201204
#if ENABLE_BINARY_PARSER
202205
auto* atlas = new Atlas(altasStr.buffer(), altasStr.length(), "", nullptr, false);
203206
if (!atlas) {
204207
return nullptr;
205208
}
206-
AttachmentLoader* attachmentLoader = new AtlasAttachmentLoaderExtension(atlas);
207-
#ifdef CC_SPINE_VERSION_3_8
208-
SkeletonBinary binary(attachmentLoader);
209-
#else
210-
SkeletonBinary binary(attachmentLoader, true);
211-
#endif
212-
binary.setScale(1.0F);
213-
SkeletonData* skeletonData = binary.readSkeletonData(s_mem, byteSize);
214-
auto& errorMsg = binary.getError();
215-
if (!errorMsg.isEmpty()) {
216-
logToConsole(errorMsg.buffer(), LOG_LEVEL_WARN);
217-
}
218-
219-
saveAttachmentVertices(skeletonData, textureNames, textureUUIDs);
209+
{
210+
AttachmentLoader* attachmentLoader = new AtlasAttachmentLoaderExtension(atlas);
211+
#ifdef CC_SPINE_VERSION_3_8
212+
SkeletonBinary binary(attachmentLoader);
213+
#else
214+
SkeletonBinary binary(attachmentLoader, true);
215+
#endif
216+
binary.setScale(1.0F);
217+
skeletonData = binary.readSkeletonData(s_mem, byteSize);
218+
auto& errorMsg = binary.getError();
219+
if (!errorMsg.isEmpty()) {
220+
logToConsole(errorMsg.buffer(), LOG_LEVEL_WARN);
221+
}
220222

221-
return skeletonData;
222-
#else
223-
return nullptr;
223+
saveAttachmentVertices(skeletonData, textureNames, textureUUIDs);
224+
}
225+
delete atlas;
224226
#endif
227+
return skeletonData;
225228
}
226229

227230
void SpineWasmUtil::registerSpineSkeletonDataWithUUID(SkeletonData* data, const String& uuid) {

native/external-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"type": "github",
44
"owner": "cocos",
55
"name": "cocos-engine-external",
6-
"checkout": "v4.0.0-1"
6+
"checkout": "v4.0.0-2"
77
}
88
}

0 commit comments

Comments
 (0)