A weird nullptr problem.
I have a character node that has two important components, one is AI to find enemy targets, one is character control to handle various things like death.
In the former AI component Update() function:
// just for this debug, it crashed at target_->GetComponent<CharacterStatus>() below previously.
if (target_) {
spdlog::trace("{}: Checking target", node_->GetName().CString());
spdlog::trace("{}: Check target: {}", node_->GetName().CString(), target_->GetName().CString());
}
// remove dead character target.
if (target_ && target_->GetComponent<CharacterStatus>()
&& target_->GetComponent<CharacterStatus>()->IsDead()) {
target_ = nullptr;
}
In the latter character control component Update() function, I will check if character is death and will trigger:
void Character::OnCharacterDeath() {
spdlog::debug("{}: Character died.", node_->GetName().CString());
node_->Remove();
}
However, when one character died, the program crashes.
The crash line is in that AI component Update() function
spdlog::trace("{}: Check target: {}", node_->GetName().CString(), target_->GetName().CString());
Particularly, the
target_->GetName()
operator
->
crashes because
const String& GetName() const { return impl_->name_; }
Notice I checked nullptr for
target_
, so it looks like the impl_ is nullptr here.
I am super confused. What did I do wrong?
I don’t have any strong pointer reference anywhere for the node except the root scene owns it.
For reference, here is the detailed log dump:
[2021-01-03 12:58:39.121] [trace] RandomAI_kBodyTeamTwo_4: Checking target
[2021-01-03 12:58:39.121] [trace] RandomAI_kBodyTeamTwo_4: Check target: RandomAI_kBodyTeamOne_3
...
[2021-01-03 12:58:39.145] [trace] RandomAI_kBodyTeamOne_3: Checking target
[2021-01-03 12:58:39.145] [trace] RandomAI_kBodyTeamOne_3: Check target: RandomAI_kBodyTeamTwo_5
[2021-01-03 12:58:39.145] [debug] RandomAI_kBodyTeamOne_3: Character died.
Assertion failed: (ptr_), function operator->, file /Users/honghaoli/git_folder/Urho3D/Source/Urho3D/AngelScript/../Core/../Container/Ptr.h, line 605.
...
[2021-01-03 12:58:39.153] [trace] RandomAI_kBodyTeamTwo_4: Checking target
Signal: SIGABRT (signal SIGABRT)
You can see the RandomAI_kBodyTeamTwo_4 first has a target RandomAI_kBodyTeamOne_3. Then, RandomAI_kBodyTeamOne_3 died, then when RandomAI_kBodyTeamTwo_4 AI component Update() again, it crashed.