Repository: godot
Issue: Usage of freed memory only in release build
Labels: bug,topic:rendering,crash

**Godot version:**
v3.2.4.rc.custom_build. b169a16cb
Looks that this may be regression from #46694
CC @JFonS 

**OS/device including version:**
Ubuntu 20.04


**Issue description:**
Exporting project with export templates compiled with this command(ubsan and CCFLAGS are not neceserry)
```
scons p=x11 -j6 tools=no target=release debug_symbols=yes use_asan=yes use_ubsan=yes CCFLAGS="-fsanitize=pointer-compare,pointer-subtract,shift,shift-exponent,integer-divide-by-zero,unreachable,vla-bound,null,return,signed-integer-overflow,bounds,float-divide-by-zero,float-cast-overflow,nonnull-attribute,returns-nonnull-attribute,bool,enum,vptr,pointer-overflow,builtin"
```
and applied this patch to speedup compilation
```
diff --git a/platform/x11/detect.py b/platform/x11/detect.py
index b8c509ad9b..a985d6bf32 100644
--- a/platform/x11/detect.py
+++ b/platform/x11/detect.py
@@ -88,11 +88,6 @@ def configure(env):
     ## Build type
 
     if env["target"] == "release":
-        if env["optimize"] == "speed":  # optimize for speed (default)
-            env.Prepend(CCFLAGS=["-O3"])
-        else:  # optimize for size
-            env.Prepend(CCFLAGS=["-Os"])
-
         if env["debug_symbols"]:
             env.Prepend(CCFLAGS=["-g2"])
```
show when running this usage after free
```
==126208==ERROR: AddressSanitizer: heap-use-after-free on address 0x617000074d80 at pc 0x00000373e666 bp 0x7ffc08b14970 sp 0x7ffc08b14960
READ of size 8 at 0x617000074d80 thread T0
    #0 0x373e665 in RID_OwnerBase::_is_owner(RID const&) const core/rid.h:110
    #1 0x373e665 in RID_Owner<RasterizerSceneGLES3::LightInstance>::owns(RID const&) const core/rid.h:178
    #2 0x373e665 in RasterizerSceneGLES3::_add_geometry_with_material(RasterizerStorageGLES3::Geometry*, RasterizerScene::InstanceBase*, RasterizerStorageGLES3::GeometryOwner*, RasterizerStorageGLES3::Material*, bool, bool) drivers/gles3/rasterizer_scene_gles3.cpp:2387
    #3 0x373e665 in RasterizerSceneGLES3::_add_geometry(RasterizerStorageGLES3::Geometry*, RasterizerScene::InstanceBase*, RasterizerStorageGLES3::GeometryOwner*, int, bool, bool) drivers/gles3/rasterizer_scene_gles3.cpp:2313
    #4 0x373e665 in RasterizerSceneGLES3::_fill_render_list(RasterizerScene::InstanceBase**, int, bool, bool) drivers/gles3/rasterizer_scene_gles3.cpp:3185
    #5 0x38bbf6d in RasterizerSceneGLES3::render_scene(Transform const&, CameraMatrix const&, bool, RasterizerScene::InstanceBase**, int, RID*, int, RID*, int, RID, RID, RID, RID, int) drivers/gles3/rasterizer_scene_gles3.cpp:4236
    #6 0xa338c8f in VisualServerScene::_render_scene(Transform, CameraMatrix const&, bool, RID, RID, RID, RID, int) servers/visual/visual_server_scene.cpp:2367
    #7 0xa27d698 in VisualServerScene::render_camera(RID, RID, Vector2, RID) servers/visual/visual_server_scene.cpp:1937
    #8 0xa4bd9a4 in VisualServerViewport::_draw_3d(VisualServerViewport::Viewport*, ARVRInterface::Eyes) servers/visual/visual_server_viewport.cpp:78
    #9 0xa4c087f in VisualServerViewport::_draw_viewport(VisualServerViewport::Viewport*, ARVRInterface::Eyes) servers/visual/visual_server_viewport.cpp:110
    #10 0xa4d2f06 in VisualServerViewport::draw_viewports() servers/visual/visual_server_viewport.cpp:348
    #11 0xa1cf2a6 in VisualServerRaster::draw(bool, double) servers/visual/visual_server_raster.cpp:108
    #12 0xa5125cc in VisualServerWrapMT::draw(bool, double) servers/visual/visual_server_wrap_mt.cpp:102
    #13 0x654ee1 in Main::iteration() main/main.cpp:2132
    #14 0x50830c in OS_X11::run() platform/x11/os_x11.cpp:3641
    #15 0x466d7b in main platform/x11/godot_x11.cpp:56
    #16 0x7fdf056fb0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
    #17 0x46699d in _start (/home/rafal/Projekty/Godot/FFF/UnnamedProject.x86_64+0x46699d)

0x617000074d80 is located 0 bytes inside of 712-byte region [0x617000074d80,0x617000075048)
freed by thread T0 here:
    #0 0x7fdf068741b7 in __interceptor_free (/lib/x86_64-linux-gnu/libasan.so.6+0xb01b7)
    #1 0xe917226 in Memory::free_static(void*, bool) core/os/memory.cpp:181
    #2 0x39392e9 in void memdelete<RasterizerSceneGLES3::LightInstance>(RasterizerSceneGLES3::LightInstance*) (/home/rafal/Projekty/Godot/FFF/UnnamedProject.x86_64+0x39392e9)
    #3 0x3903667 in RasterizerSceneGLES3::free(RID) drivers/gles3/rasterizer_scene_gles3.cpp:4977
    #4 0xa229cfc in VisualServerScene::instance_set_base(RID, RID) servers/visual/visual_server_scene.cpp:496
    #5 0xa2052de in VisualServerRaster::instance_set_base(RID, RID) servers/visual/visual_server_raster.h:546
    #6 0xa5ebd77 in VisualServerWrapMT::instance_set_base(RID, RID) servers/visual/visual_server_wrap_mt.h:468
    #7 0x6386566 in Light::~Light() scene/3d/light.cpp:341
    #8 0x63a9bfa in DirectionalLight::~DirectionalLight() scene/3d/light.h:135
    #9 0x4a28307 in void memdelete<Node>(Node*) core/os/memory.h:117
    #10 0x499e8fd in Node::_notification(int) scene/main/node.cpp:175
    #11 0x68311e in Node::_notificationv(int, bool) scene/main/node.h:46
    #12 0x685a08 in Spatial::_notificationv(int, bool) (/home/rafal/Projekty/Godot/FFF/UnnamedProject.x86_64+0x685a08)
    #13 0xe151187 in Object::notification(int, bool) core/object.cpp:929
    #14 0xe13c6f9 in Object::_predelete() core/object.cpp:387
    #15 0xe17d37c in predelete_handler(Object*) core/object.cpp:2055
    #16 0x4a28149 in void memdelete<Node>(Node*) core/os/memory.h:114
    #17 0x499e8fd in Node::_notification(int) scene/main/node.cpp:175
    #18 0x68311e in Node::_notificationv(int, bool) scene/main/node.h:46
    #19 0xe151187 in Object::notification(int, bool) core/object.cpp:929
    #20 0xe13c6f9 in Object::_predelete() core/object.cpp:387
    #21 0xe17d37c in predelete_handler(Object*) core/object.cpp:2055
    #22 0x65c80c in void memdelete<Object>(Object*) core/os/memory.h:114
    #23 0x4aed9de in SceneTree::_flush_delete_queue() scene/main/scene_tree.cpp:1117
    #24 0x4ad6ace in SceneTree::idle(float) scene/main/scene_tree.cpp:547
    #25 0x654184 in Main::iteration() main/main.cpp:2117
    #26 0x50830c in OS_X11::run() platform/x11/os_x11.cpp:3641
    #27 0x466d7b in main platform/x11/godot_x11.cpp:56
    #28 0x7fdf056fb0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)

previously allocated by thread T0 here:
    #0 0x7fdf06874517 in malloc (/lib/x86_64-linux-gnu/libasan.so.6+0xb0517)
    #1 0xe916d35 in Memory::alloc_static(unsigned long, bool) core/os/memory.cpp:82
    #2 0xe916cd2 in operator new(unsigned long, char const*) core/os/memory.cpp:42
    #3 0x3645ec8 in RasterizerSceneGLES3::light_instance_create(RID) drivers/gles3/rasterizer_scene_gles3.cpp:1001
    #4 0xa22ee53 in VisualServerScene::instance_set_base(RID, RID) servers/visual/visual_server_scene.cpp:572
    #5 0xa2052de in VisualServerRaster::instance_set_base(RID, RID) servers/visual/visual_server_raster.h:546
    #6 0xa5ebd77 in VisualServerWrapMT::instance_set_base(RID, RID) servers/visual/visual_server_wrap_mt.h:468
    #7 0x6383785 in Light::Light(VisualServer::LightType) scene/3d/light.cpp:305
    #8 0x638c952 in DirectionalLight::DirectionalLight() scene/3d/light.cpp:411
    #9 0x48eb933 in Object* ClassDB::creator<DirectionalLight>() (/home/rafal/Projekty/Godot/FFF/UnnamedProject.x86_64+0x48eb933)
    #10 0xdeb1226 in ClassDB::instance(StringName const&) core/class_db.cpp:559
    #11 0x1673f4a in GDScriptNativeClass::instance() modules/gdscript/gdscript.cpp:82
    #12 0x1673bee in GDScriptNativeClass::_new() modules/gdscript/gdscript.cpp:69
    #13 0x16f51e4 in MethodBind0R<Variant>::call(Object*, Variant const**, int, Variant::CallError&) core/method_bind.gen.inc:237
    #14 0xe150b35 in Object::call(StringName const&, Variant const**, int, Variant::CallError&) core/object.cpp:919
    #15 0xe51cd9a in Variant::call_ptr(StringName const&, Variant const**, int, Variant*, Variant::CallError&) core/variant_call.cpp:1148
    #16 0x1818e18 in GDScriptFunction::call(GDScriptInstance*, Variant const**, int, Variant::CallError&, GDScriptFunction::CallState*) modules/gdscript/gdscript_function.cpp:1083
    #17 0x169acf6 in GDScriptInstance::call(StringName const&, Variant const**, int, Variant::CallError&) modules/gdscript/gdscript.cpp:1208
    #18 0xe150188 in Object::call(StringName const&, Variant const**, int, Variant::CallError&) core/object.cpp:898
    #19 0xe51cd9a in Variant::call_ptr(StringName const&, Variant const**, int, Variant*, Variant::CallError&) core/variant_call.cpp:1148
    #20 0x1818e99 in GDScriptFunction::call(GDScriptInstance*, Variant const**, int, Variant::CallError&, GDScriptFunction::CallState*) modules/gdscript/gdscript_function.cpp:1086
    #21 0x169bb4f in GDScriptInstance::_ml_call_reversed(GDScript*, StringName const&, Variant const**, int) modules/gdscript/gdscript.cpp:1239
    #22 0x169beed in GDScriptInstance::call_multilevel_reversed(StringName const&, Variant const**, int) modules/gdscript/gdscript.cpp:1246
    #23 0x499ced7 in Node::_notification(int) scene/main/node.cpp:149
    #24 0x68311e in Node::_notificationv(int, bool) scene/main/node.h:46
    #25 0x685552 in Spatial::_notificationv(int, bool) (/home/rafal/Projekty/Godot/FFF/UnnamedProject.x86_64+0x685552)
    #26 0xe151187 in Object::notification(int, bool) core/object.cpp:929
    #27 0x49a05f3 in Node::_propagate_ready() scene/main/node.cpp:196
    #28 0x499fe59 in Node::_propagate_ready() scene/main/node.cpp:188
    #29 0x4a0c75e in Node::_set_tree(SceneTree*) scene/main/node.cpp:2637

SUMMARY: AddressSanitizer: heap-use-after-free core/rid.h:110 in RID_OwnerBase::_is_owner(RID const&) const
```

**Minimal reproduction project:**
https://github.com/qarmin/RegressionTestProject/archive/3.2.zip