Fix a potential crash after destroying a node with Spine Skeleton Component in schedule update on native platforms. (#18567)

This commit is contained in:
bofeng-song
2025-04-09 14:59:51 +08:00
committed by GitHub
parent b0dbb458fa
commit a9c82270ee
3 changed files with 18 additions and 16 deletions
@@ -80,16 +80,18 @@ void MiddlewareManager::update(float dt) {
auto *editor = _updateList[i];
editor->update(dt);
}
for (auto &iter: _operateCacheMap) {
auto it = std::find(_updateList.begin(), _updateList.end(), iter.first);
if (!iter.second && it != _updateList.end()) {
_updateList.erase(it);
}
}
}
void MiddlewareManager::render(float dt) {
// Object._deferredDestroy is called after component update in Director.tick and before emitting BEFORE_DRAW event in which MiddlewareManager::render is invoked,
// so the native object may be released here and it needs to be erased from _updateList.
for (auto &iter : _operateCacheMap) {
auto it = std::find(_updateList.begin(), _updateList.end(), iter.first);
if (!iter.second && it != _updateList.end()) {
_updateList.erase(it);
}
}
for (auto it : _mbMap) {
auto *buffer = it.second;
if (buffer) {
@@ -97,7 +99,6 @@ void MiddlewareManager::render(float dt) {
}
}
for (size_t i = 0, len = _updateList.size(); i < len; ++i) {
auto *editor = _updateList[i];
editor->render(dt);
@@ -159,8 +159,8 @@ void SkeletonCacheAnimation::render(float /*dt*/) {
if (!_animationData) return;
SkeletonCache::FrameData *frameData = _animationData->getFrameData(_curFrameIndex);
if (!frameData) return;
auto *entity = _entity;
entity->clearDynamicRenderDrawInfos();
if (!_entity || !_entity->getNode()) return;
_entity->clearDynamicRenderDrawInfos();
const auto &segments = frameData->getSegments();
const auto &colors = frameData->getColors();
@@ -197,7 +197,7 @@ void SkeletonCacheAnimation::render(float /*dt*/) {
int vs = _useTint ? vs2 : vs1;
int vbs = _useTint ? vbs2 : vbs1;
auto &nodeWorldMat = entity->getNode()->getWorldMatrix();
auto &nodeWorldMat = _entity->getNode()->getWorldMatrix();
int colorOffset = 0;
SkeletonCache::ColorData *nowColor = colors[colorOffset++];
@@ -269,7 +269,7 @@ void SkeletonCacheAnimation::render(float /*dt*/) {
vertexFloats = segment->vertexFloatCount;
}
curDrawInfo = requestDrawInfo(segmentCount++);
entity->addDynamicRenderDrawInfo(curDrawInfo);
_entity->addDynamicRenderDrawInfo(curDrawInfo);
// fill new texture index
curTexture = static_cast<cc::Texture2D *>(segment->getTexture()->getRealTexture());
gfx::Texture *texture = curTexture->getGFXTexture();
@@ -271,8 +271,9 @@ void SkeletonRenderer::initWithBinaryFile(const std::string &skeletonDataFile, c
void SkeletonRenderer::render(float /*deltaTime*/) {
if (!_skeleton) return;
auto *entity = _entity;
entity->clearDynamicRenderDrawInfos();
// entity's node may be set to nullptr while component is destroyed.
if (!_entity || !_entity->getNode()) return;
_entity->clearDynamicRenderDrawInfos();
_sharedBufferOffset->reset();
_sharedBufferOffset->clear();
@@ -289,7 +290,7 @@ void SkeletonRenderer::render(float /*deltaTime*/) {
if (_skeleton->getColor().a == 0) {
return;
}
auto &nodeWorldMat = entity->getNode()->getWorldMatrix();
auto &nodeWorldMat = _entity->getNode()->getWorldMatrix();
// color range is [0.0, 1.0]
cc::middleware::Color4F color;
cc::middleware::Color4F darkColor;
@@ -344,7 +345,7 @@ void SkeletonRenderer::render(float /*deltaTime*/) {
curDrawInfo->setIbCount(curISegLen);
}
curDrawInfo = requestDrawInfo(materialLen);
entity->addDynamicRenderDrawInfo(curDrawInfo);
_entity->addDynamicRenderDrawInfo(curDrawInfo);
// prepare to fill new segment field
curBlendMode = slot->getData().getBlendMode();
switch (curBlendMode) {