From 16aeb84cd35996a6b41f10cbc48a677eeccc911c Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sat, 2 Jan 2021 13:50:34 +0000 Subject: Fix potential destruction crashes (#5095) * Fix potential destruction crashes * Fix destructors accessing destroyted objects * Fix cPlayer not destroying windows (Destroyed never called) * Tentatively fixes #4608, fixes #3236, fixes #3262 - Remove cEntity::Destroyed() and replace with cEntity::OnRemoveFromWorld() * Add missing call to OnRemoveFromWorld --- src/BlockEntities/ChestEntity.cpp | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'src/BlockEntities/ChestEntity.cpp') diff --git a/src/BlockEntities/ChestEntity.cpp b/src/BlockEntities/ChestEntity.cpp index 8c8e75b25..e225cf96e 100644 --- a/src/BlockEntities/ChestEntity.cpp +++ b/src/BlockEntities/ChestEntity.cpp @@ -32,31 +32,31 @@ cChestEntity::cChestEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector -cChestEntity::~cChestEntity() +void cChestEntity::CopyFrom(const cBlockEntity & a_Src) { - if (m_Neighbour != nullptr) - { - // Neighbour may share a window with us, force the window shut - m_Neighbour->DestroyWindow(); - m_Neighbour->m_Neighbour = nullptr; - } + Super::CopyFrom(a_Src); + auto & src = static_cast(a_Src); + m_Contents.CopyFrom(src.m_Contents); - DestroyWindow(); + // Reset the neighbor and player count, there's no sense in copying these: + m_Neighbour = nullptr; + m_NumActivePlayers = 0; } -void cChestEntity::CopyFrom(const cBlockEntity & a_Src) +void cChestEntity::OnRemoveFromWorld() { - Super::CopyFrom(a_Src); - auto & src = static_cast(a_Src); - m_Contents.CopyFrom(src.m_Contents); + if (m_Neighbour != nullptr) + { + // Neighbour may share a window with us, force the window shut: + m_Neighbour->DestroyWindow(); + m_Neighbour->m_Neighbour = nullptr; + } - // Reset the neighbor and player count, there's no sense in copying these: - m_Neighbour = nullptr; - m_NumActivePlayers = 0; + DestroyWindow(); } @@ -199,11 +199,10 @@ void cChestEntity::OpenNewWindow(void) void cChestEntity::DestroyWindow() { - cWindow * Window = GetWindow(); + const auto Window = GetWindow(); if (Window != nullptr) { Window->OwnerDestroyed(); - CloseWindow(); } } -- cgit v1.2.3