Skip to content

Commit

Permalink
fix crash on destroy AttachableObject
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Mar 23, 2024
1 parent 70d0579 commit 229652f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/client/attachableobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ extern ParticleManager g_particles;

AttachableObject::~AttachableObject()
{
clearAttachedEffects();
clearAttachedEffects(false);
clearAttachedParticlesEffect();
clearAttachedWidgets();
clearAttachedWidgets(false);
}

void AttachableObject::attachEffect(const AttachedEffectPtr& obj)
Expand Down Expand Up @@ -93,20 +93,21 @@ bool AttachableObject::detachEffectById(uint16_t id)
return true;
}

void AttachableObject::onDetachEffect(const AttachedEffectPtr& effect)
void AttachableObject::onDetachEffect(const AttachedEffectPtr& effect, bool callEvent)
{
if (effect->isHidedOwner())
--m_ownerHidden;

onStartDetachEffect(effect);

effect->callLuaField("onDetach", attachedObjectToLuaObject());
if (callEvent)
effect->callLuaField("onDetach", attachedObjectToLuaObject());
}

void AttachableObject::clearAttachedEffects()
void AttachableObject::clearAttachedEffects(bool callEvent)
{
for (const auto& e : m_attachedEffects)
onDetachEffect(e);
onDetachEffect(e, callEvent);
m_attachedEffects.clear();
}

Expand Down Expand Up @@ -272,7 +273,7 @@ bool AttachableObject::detachWidget(const UIWidgetPtr& widget)
return true;
}

void AttachableObject::clearAttachedWidgets()
void AttachableObject::clearAttachedWidgets(bool callEvent)
{
// keep the same behavior as detachWidget
auto oldList = std::move(m_attachedWidgets);
Expand All @@ -281,7 +282,9 @@ void AttachableObject::clearAttachedWidgets()
for (const auto& widget : oldList) {
g_map.removeAttachedWidgetFromObject(widget);
widget->removeOnDestroyCallback("attached-widget-destroy");
widget->callLuaField("onDetached", asLuaObject());

if (callEvent)
widget->callLuaField("onDetached", asLuaObject());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/client/attachableobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AttachableObject : public LuaObject
virtual bool isThing() { return false; }

void attachEffect(const AttachedEffectPtr& obj);
void clearAttachedEffects();
void clearAttachedEffects(bool callEvent = true);
void clearTemporaryAttachedEffects();
void clearPermanentAttachedEffects();
bool detachEffectById(uint16_t id);
Expand All @@ -59,14 +59,14 @@ class AttachableObject : public LuaObject
bool hasAttachedWidgets() { return !m_attachedWidgets.empty(); };
bool isWidgetAttached(const UIWidgetPtr& widget);
void attachWidget(const UIWidgetPtr& widget);
void clearAttachedWidgets();
void clearAttachedWidgets(bool callEvent = true);
bool detachWidgetById(const std::string& id);
bool detachWidget(const UIWidgetPtr& widget);
UIWidgetPtr getAttachedWidgetById(const std::string& id);

protected:
void drawAttachedEffect(const Point& dest, LightView* lightView, bool isOnTop);
void onDetachEffect(const AttachedEffectPtr& effect);
void onDetachEffect(const AttachedEffectPtr& effect, bool callEvent = true);
void drawAttachedParticlesEffect(const Point& dest);

std::vector<AttachedEffectPtr> m_attachedEffects;
Expand Down

0 comments on commit 229652f

Please sign in to comment.