### Godot version

v4.0 rc6

### System information

Windows 10

### Issue description

When using a ViewportTexture from a SubViewport on a TextureRect, it throws errors after running the scene once and then switching scenes in the editor.
![Pasted image 20230227212626](https://user-images.githubusercontent.com/44676282/221680047-984fa03e-33d1-4856-9a7f-1700b28423cf.png)


### Steps to reproduce

Run the canvas-scene once.
Switch to another scene in the editor.
Switch back to the canvas-scene

### Minimal reproduction project

[ViewportTextureError.zip](https://github.com/godotengine/godot/files/10844099/ViewportTextureError.zip)

SubWindow sw = gui.sub_windows.write[i];
			
			Rect2i r = Rect2i(sw.window->get_position(), sw.window->get_size());
			if (!sw.window->get_flag(Window::FLAG_BORDERLESS)) {
				
				int title_height = sw.window->get_theme_constant(SNAME("title_height"));
				Rect2i title_bar = r;
				title_bar.position.y -= title_height;
				title_bar.size.y = title_height;
				if (title_bar.size.y > 0 && title_bar.has_point(mb->get_position())) {
					click_on_window = sw.window;
					int close_h_ofs = sw.window->get_theme_constant(SNAME("close_h_offset"));
					int close_v_ofs = sw.window->get_theme_constant(SNAME("close_v_offset"));
					Ref<Texture2D> close_icon = sw.window->get_theme_icon(SNAME("close"));
					Rect2 close_rect;
					close_rect.position = Vector2(r.position.x + r.size.x - close_h_ofs, r.position.y - close_v_ofs);
					close_rect.size = close_icon->get_size();
					if (gui.subwindow_focused != sw.window) {
						
						_sub_window_grab_focus(sw.window);
					}
					if (close_rect.has_point(mb->get_position())) {
						gui.subwindow_drag = SUB_WINDOW_DRAG_CLOSE;
						gui.subwindow_drag_close_inside = true; 
						gui.subwindow_drag_close_rect = close_rect;
					} else {
						gui.subwindow_drag = SUB_WINDOW_DRAG_MOVE;
					}
					gui.subwindow_drag_from = mb->get_position();
					gui.subwindow_drag_pos = sw.window->get_position();