Repository: godot
Issue: Editor crashes if an autoloaded tool script frees an autoload
Labels: topic:core,documentation,crash

**Godot version:**
3.2.1 245ecb6684d089940273564fb7a314e5e11ea72e

**OS/device including version:**
Win10 64-bit

**Issue description:**
I isolated a small bit of code in an Autoload will frequently and randomly crash the editor.

```
tool
extends CanvasLayer


func _ready() -> void:

	if(Engine.editor_hint):

		if(get_tree().edited_scene_root != self):
			queue_free()

		return

	print("Load normally.")
```

Trace

```
CrashHandlerException: Program crashed
Dumping the backtrace. Please include this when reporting the bug on https://github.com/godotengine/godot/issues
[0] RaiseException
[1] _CxxThrowException (d:\agent\_work\5\s\src\vctools\crt\vcruntime\src\eh\throw.cpp:75)
[2] __RTDynamicCast (d:\agent\_work\5\s\src\vctools\crt\vcruntime\src\eh\rtti.cpp:291)
[3] Object::cast_to<Script> (C:\Godot\Version 3.2\Source\core\object.h:610)
[4] Ref<Script>::Ref<Script> (C:\Godot\Version 3.2\Source\core\reference.h:264)
[5] EditorAutoloadSettings::update_autoload (C:\Godot\Version 3.2\Source\editor\editor_autoload_settings.cpp:426)
[6] ProjectSettingsEditor::popup_project_settings (C:\Godot\Version 3.2\Source\editor\project_settings_editor.cpp:811)
[7] EditorNode::_menu_option_confirm (C:\Godot\Version 3.2\Source\editor\editor_node.cpp:2467)
[8] EditorNode::_menu_option (C:\Godot\Version 3.2\Source\editor\editor_node.cpp:863)
[9] MethodBind1<EditorNode,int>::call (C:\Godot\Version 3.2\Source\core\method_bind.gen.inc:867)
[10] Object::call (C:\Godot\Version 3.2\Source\core\object.cpp:921)
[11] Object::emit_signal (C:\Godot\Version 3.2\Source\core\object.cpp:1217)
[12] Object::emit_signal (C:\Godot\Version 3.2\Source\core\object.cpp:1275)
[13] PopupMenu::activate_item (C:\Godot\Version 3.2\Source\scene\gui\popup_menu.cpp:1162)
[14] PopupMenu::_gui_input (C:\Godot\Version 3.2\Source\scene\gui\popup_menu.cpp:343)
[15] MethodBind1<PopupMenu,Ref<InputEvent> const &>::call (C:\Godot\Version 3.2\Source\core\method_bind.gen.inc:867)
[16] Object::call_multilevel (C:\Godot\Version 3.2\Source\core\object.cpp:763)
[17] Object::call_multilevel (C:\Godot\Version 3.2\Source\core\object.cpp:864)
[18] Viewport::_gui_call_input (C:\Godot\Version 3.2\Source\scene\main\viewport.cpp:1669)
[19] Viewport::_gui_input_event (C:\Godot\Version 3.2\Source\scene\main\viewport.cpp:2049)
[20] Viewport::input (C:\Godot\Version 3.2\Source\scene\main\viewport.cpp:2828)
[21] Viewport::_vp_input (C:\Godot\Version 3.2\Source\scene\main\viewport.cpp:1446)
[22] MethodBind1<Viewport,Ref<InputEvent> const &>::call (C:\Godot\Version 3.2\Source\core\method_bind.gen.inc:867)
[23] Object::call (C:\Godot\Version 3.2\Source\core\object.cpp:921)
[24] Object::call (C:\Godot\Version 3.2\Source\core\object.cpp:847)
[25] SceneTree::call_group_flags (C:\Godot\Version 3.2\Source\scene\main\scene_tree.cpp:275)
[26] SceneTree::input_event (C:\Godot\Version 3.2\Source\scene\main\scene_tree.cpp:431)
[27] InputDefault::_parse_input_event_impl (C:\Godot\Version 3.2\Source\main\input_default.cpp:442)
[28] InputDefault::parse_input_event (C:\Godot\Version 3.2\Source\main\input_default.cpp:260)
[29] InputDefault::flush_accumulated_events (C:\Godot\Version 3.2\Source\main\input_default.cpp:679)
[30] OS_Windows::process_events (C:\Godot\Version 3.2\Source\platform\windows\os_windows.cpp:2464)
[31] OS_Windows::run (C:\Godot\Version 3.2\Source\platform\windows\os_windows.cpp:3159)
[32] widechar_main (C:\Godot\Version 3.2\Source\platform\windows\godot_windows.cpp:162)
[33] _main (C:\Godot\Version 3.2\Source\platform\windows\godot_windows.cpp:184)
[34] main (C:\Godot\Version 3.2\Source\platform\windows\godot_windows.cpp:196)
[35] __scrt_common_main_seh (d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288)
[36] BaseThreadInitThunk
-- END OF BACKTRACE --
```

@vnen Wondering if you feel this is something that is a thing not to do, or is something that's supposed to be supported?



**Steps to reproduce:**
Use the project below.  I'm unsure how exactly it is triggered, but open and close the project repeatedly, or move around and do things, save things, start and stop the app, etc.  It occurs periodically.

Reproduced most often when I opened the Project Settings.  Though it crashes sometimes when the project is opened.

**Minimal reproduction project:**
[Autoload Crash.zip](https://github.com/godotengine/godot/files/4215851/Autoload.Crash.zip)