Skip to content

Commit

Permalink
Fix invalid access crash on exit in LimboHSM
Browse files Browse the repository at this point in the history
Since #131, `LimboState::_exit()` became a source of potential crashes
if object references are used without a validity check. It's too easy
to miss this, which can lead to game crashing during runtime.

This fix reverts #131 change and proposes alternative approach of
re-activating root HSM upon tree entering if it was previously active.
Note that it's not an ideal solution, as some state will be lost upon
re-parenting: HSM exits and then re-activates and enters its initial state.
  • Loading branch information
limbonaut committed Sep 22, 2024
1 parent 134bb32 commit 60142b1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
15 changes: 15 additions & 0 deletions hsm/limbo_hsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,21 @@ void LimboHSM::_validate_property(PropertyInfo &p_property) const {
void LimboHSM::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_POST_ENTER_TREE: {
if (was_active && is_root()) {
// Re-activate the root HSM if it was previously active.
// Typically, this happens when the node is re-entered scene repeatedly (e.g., re-parenting, pooling).
set_active(true);
}
} break;
case NOTIFICATION_EXIT_TREE: {
if (is_root()) {
// Remember active status for re-parenting and exit state machine
// to release resources and signal connections if active.
was_active = active;
if (is_active()) {
_exit();
}
}
} break;
case NOTIFICATION_PROCESS: {
_update(get_process_delta_time());
Expand Down
1 change: 1 addition & 0 deletions hsm/limbo_hsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class LimboHSM : public LimboState {
LimboState *previous_active;
LimboState *next_active;
bool updating = false;
bool was_active = false;

HashMap<TransitionKey, Transition, TransitionKeyHasher> transitions;

Expand Down
5 changes: 0 additions & 5 deletions hsm/limbo_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,6 @@ void LimboState::_notification(int p_what) {
_update_blackboard_plan();
}
} break;
case NOTIFICATION_PREDELETE: {
if (is_active()) {
_exit();
}
} break;
}
}

Expand Down

0 comments on commit 60142b1

Please sign in to comment.