diff --git a/lib/nw/kernel/Objects.cpp b/lib/nw/kernel/Objects.cpp index b6dadf180..c65c261ca 100644 --- a/lib/nw/kernel/Objects.cpp +++ b/lib/nw/kernel/Objects.cpp @@ -10,9 +10,20 @@ namespace nw::kernel { void ObjectSystem::clear() { + // Clear tag map first + object_tag_map_.clear(); + + // Make sure all objects are correctly destroyed. Have to do this now that + // the underlying storage is a memory pool that will not itself run destructor. + for (auto& obj : objects_) { + if (obj.is()) { + destroy(obj.as()->handle()); + } + } + + // Clear the free list free_list_ = std::stack>(); objects_.clear(); - object_tag_map_.clear(); module_.reset(); areas_.clear(); creatures_.clear(); @@ -78,7 +89,7 @@ void ObjectSystem::destroy(ObjectHandle obj) { if (valid(obj)) { size_t idx = static_cast(obj.id); - auto o = std::get(objects_[idx]); + auto o = objects_[idx].as(); auto new_handle = o->handle(); // Delete from tag map @@ -146,7 +157,7 @@ ObjectBase* ObjectSystem::get_object_base(ObjectHandle obj) const { if (!valid(obj)) { return nullptr; } auto idx = static_cast(obj.id); - return std::get(objects_[idx]); + return objects_[idx].as(); } ObjectBase* ObjectSystem::get_by_tag(std::string_view tag, int nth) const @@ -236,11 +247,11 @@ void ObjectSystem::set_instantiate_callback(void (*callback)(ObjectBase*)) bool ObjectSystem::valid(ObjectHandle handle) const { auto idx = static_cast(handle.id); - if (idx >= objects_.size() || std::holds_alternative(objects_[idx])) { + if (idx >= objects_.size() || objects_[idx].is()) { return false; } - if (auto& obj = std::get(objects_[idx])) { + if (auto obj = objects_[idx].as()) { return obj->handle() == handle; } diff --git a/lib/nw/kernel/Objects.hpp b/lib/nw/kernel/Objects.hpp index 43c8c7cda..d1633bebe 100644 --- a/lib/nw/kernel/Objects.hpp +++ b/lib/nw/kernel/Objects.hpp @@ -27,7 +27,7 @@ namespace nw::kernel { -using ObjectPayload = std::variant; +using ObjectPayload = Variant; struct ObjectSystemStats { size_t total_objects = 0; @@ -156,7 +156,7 @@ T* ObjectSystem::get(ObjectHandle obj) { if (!valid(obj) || T::object_type != obj.type) return nullptr; auto idx = static_cast(obj.id); - return static_cast(std::get(objects_[idx])); + return static_cast(objects_[idx].as()); } template @@ -169,7 +169,7 @@ T* ObjectSystem::make() auto oid = free_list_.top(); auto idx = static_cast(oid); free_list_.pop(); - ObjectHandle oh = std::get(objects_[idx]); + ObjectHandle oh = objects_[idx].as(); oh.type = T::object_type; obj->set_handle(oh); objects_[idx] = obj;