From 356fc728a491fef3b791bbe3b7bccbde214c028f Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Mon, 14 Aug 2023 11:23:16 +0100 Subject: [PATCH] Fix `TileMapEditorPlugin` crash by storing tilemap ID instead of pointer Store the tilemap ObjectID instead of raw pointer, and check it is valid before access. --- editor/plugins/tiles/tiles_editor_plugin.cpp | 7 +++++++ editor/plugins/tiles/tiles_editor_plugin.h | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/editor/plugins/tiles/tiles_editor_plugin.cpp b/editor/plugins/tiles/tiles_editor_plugin.cpp index 121b70a74fe3..e432704702b7 100644 --- a/editor/plugins/tiles/tiles_editor_plugin.cpp +++ b/editor/plugins/tiles/tiles_editor_plugin.cpp @@ -325,6 +325,7 @@ void TileMapEditorPlugin::_tile_map_changed() { } void TileMapEditorPlugin::_update_tile_map() { + TileMap *tile_map = Object::cast_to(ObjectDB::get_instance(tile_map_id)); if (tile_map) { Ref tile_set = tile_map->get_tileset(); if (tile_set.is_valid() && edited_tileset != tile_set->get_instance_id()) { @@ -347,11 +348,17 @@ void TileMapEditorPlugin::_notification(int p_notification) { } void TileMapEditorPlugin::edit(Object *p_object) { + TileMap *tile_map = Object::cast_to(ObjectDB::get_instance(tile_map_id)); if (tile_map) { tile_map->disconnect("changed", callable_mp(this, &TileMapEditorPlugin::_tile_map_changed)); } tile_map = Object::cast_to(p_object); + if (tile_map) { + tile_map_id = tile_map->get_instance_id(); + } else { + tile_map_id = ObjectID(); + } editor->edit(tile_map); if (tile_map) { diff --git a/editor/plugins/tiles/tiles_editor_plugin.h b/editor/plugins/tiles/tiles_editor_plugin.h index f8e944af8184..81cb48eb00a1 100644 --- a/editor/plugins/tiles/tiles_editor_plugin.h +++ b/editor/plugins/tiles/tiles_editor_plugin.h @@ -115,7 +115,7 @@ class TileMapEditorPlugin : public EditorPlugin { TileMapEditor *editor = nullptr; Button *button = nullptr; - TileMap *tile_map = nullptr; + ObjectID tile_map_id; bool tile_map_changed_needs_update = false; ObjectID edited_tileset;