From 05c33acbb1e7189719d1edd6105cbb23065bdd9d Mon Sep 17 00:00:00 2001 From: Lukas Tenbrink Date: Thu, 23 Oct 2025 18:21:08 +0200 Subject: [PATCH] Make `memnew(RefCounted)` return `Ref`, to force callers to take ownership of it through a reference. --- core/debugger/engine_debugger.cpp | 4 +- core/debugger/engine_debugger.h | 3 +- core/debugger/remote_debugger_peer.cpp | 4 +- core/debugger/remote_debugger_peer.h | 4 +- core/io/net_socket.cpp | 4 +- core/io/net_socket.h | 4 +- core/io/resource_format_binary.cpp | 8 +- core/io/udp_server.cpp | 2 - core/io/udp_server.h | 2 +- core/math/static_raycaster.cpp | 2 +- core/math/static_raycaster.h | 2 +- core/object/ref_counted.h | 14 ++- core/os/memory.h | 15 ++- drivers/unix/net_socket_unix.cpp | 7 +- drivers/unix/net_socket_unix.h | 2 +- drivers/windows/net_socket_winsock.cpp | 4 +- drivers/windows/net_socket_winsock.h | 2 +- .../animation_blend_space_1d_editor.cpp | 2 +- .../animation_blend_space_2d_editor.cpp | 2 +- .../animation_player_editor_plugin.h | 4 +- editor/debugger/editor_debugger_server.cpp | 10 +- editor/debugger/editor_debugger_server.h | 4 +- editor/import/3d/resource_importer_scene.cpp | 12 ++- editor/import/3d/scene_import_settings.cpp | 5 +- editor/import/3d/scene_import_settings.h | 4 +- .../project_converter_3_to_4.cpp | 92 +++++-------------- .../project_converter_3_to_4.h | 8 +- editor/scene/3d/node_3d_editor_plugin.cpp | 1 - editor/scene/3d/node_3d_editor_plugin.h | 2 +- editor/scene/3d/skeleton_3d_editor_plugin.cpp | 3 +- editor/scene/3d/skeleton_3d_editor_plugin.h | 2 +- editor/script/script_editor_plugin.cpp | 6 +- modules/gdscript/gdscript.cpp | 13 +-- modules/gltf/gltf_document.cpp | 3 +- modules/lightmapper_rd/register_types.cpp | 2 +- modules/mbedtls/crypto_mbedtls.cpp | 13 +-- modules/mbedtls/crypto_mbedtls.h | 4 +- modules/mbedtls/tls_context_mbedtls.cpp | 6 +- modules/raycast/lightmap_raycaster_embree.cpp | 2 +- modules/raycast/lightmap_raycaster_embree.h | 2 +- modules/raycast/static_raycaster_embree.cpp | 2 +- modules/raycast/static_raycaster_embree.h | 2 +- modules/theora/video_stream_theora.cpp | 3 +- .../editor_debugger_server_websocket.cpp | 4 +- .../editor/editor_debugger_server_websocket.h | 2 +- .../remote_debugger_peer_websocket.cpp | 6 +- .../remote_debugger_peer_websocket.h | 2 +- platform/android/net_socket_android.cpp | 2 +- platform/android/net_socket_android.h | 2 +- platform/linuxbsd/wayland/wayland_thread.cpp | 4 +- platform/web/net_socket_web.h | 2 +- scene/3d/lightmapper.cpp | 12 +-- scene/3d/lightmapper.h | 6 +- scene/resources/2d/tile_set.cpp | 5 +- scene/resources/resource_format_text.cpp | 16 ++-- tests/core/io/test_resource.cpp | 2 +- tests/core/io/test_stream_peer_tcp.cpp | 4 +- tests/scene/test_packed_scene.cpp | 9 +- 58 files changed, 174 insertions(+), 196 deletions(-) diff --git a/core/debugger/engine_debugger.cpp b/core/debugger/engine_debugger.cpp index ee0a06a0c3..c51903108f 100644 --- a/core/debugger/engine_debugger.cpp +++ b/core/debugger/engine_debugger.cpp @@ -138,8 +138,8 @@ void EngineDebugger::initialize(const String &p_uri, bool p_skip_breakpoints, bo CreatePeerFunc *create_fn = protocols.getptr(proto); ERR_FAIL_NULL_MSG(create_fn, vformat("Invalid protocol: %s.", proto)); - RemoteDebuggerPeer *peer = (*create_fn)(p_uri); - if (!peer) { + Ref peer = (*create_fn)(p_uri); + if (peer.is_null()) { return; } singleton = memnew(RemoteDebugger(Ref(peer))); diff --git a/core/debugger/engine_debugger.h b/core/debugger/engine_debugger.h index 95b64458f6..80deb1f46a 100644 --- a/core/debugger/engine_debugger.h +++ b/core/debugger/engine_debugger.h @@ -30,6 +30,7 @@ #pragma once +#include "core/object/ref_counted.h" #include "core/string/string_name.h" #include "core/string/ustring.h" #include "core/templates/hash_map.h" @@ -47,7 +48,7 @@ public: typedef Error (*CaptureFunc)(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured); - typedef RemoteDebuggerPeer *(*CreatePeerFunc)(const String &p_uri); + typedef Ref (*CreatePeerFunc)(const String &p_uri); class Profiler { friend class EngineDebugger; diff --git a/core/debugger/remote_debugger_peer.cpp b/core/debugger/remote_debugger_peer.cpp index 19dea3ec04..bb79104476 100644 --- a/core/debugger/remote_debugger_peer.cpp +++ b/core/debugger/remote_debugger_peer.cpp @@ -211,7 +211,7 @@ void RemoteDebuggerPeerTCP::_poll() { } } -RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create_tcp(const String &p_uri) { +Ref RemoteDebuggerPeerTCP::create_tcp(const String &p_uri) { ERR_FAIL_COND_V(!p_uri.begins_with("tcp://"), nullptr); String debug_host = p_uri.replace("tcp://", ""); @@ -237,7 +237,7 @@ RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create_tcp(const String &p_uri) { return memnew(RemoteDebuggerPeerTCP(stream)); } -RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create_unix(const String &p_uri) { +Ref RemoteDebuggerPeerTCP::create_unix(const String &p_uri) { ERR_FAIL_COND_V(!p_uri.begins_with("unix://"), nullptr); String debug_path = p_uri.replace("unix://", ""); diff --git a/core/debugger/remote_debugger_peer.h b/core/debugger/remote_debugger_peer.h index dec7d91106..cf7adfbf4c 100644 --- a/core/debugger/remote_debugger_peer.h +++ b/core/debugger/remote_debugger_peer.h @@ -82,8 +82,8 @@ private: static Error _try_connect(Ref p_stream); public: - static RemoteDebuggerPeer *create_tcp(const String &p_uri); - static RemoteDebuggerPeer *create_unix(const String &p_uri); + static Ref create_tcp(const String &p_uri); + static Ref create_unix(const String &p_uri); bool is_peer_connected() override; int get_max_message_size() const override; diff --git a/core/io/net_socket.cpp b/core/io/net_socket.cpp index f7a2c5e38c..e5bdc4d1b9 100644 --- a/core/io/net_socket.cpp +++ b/core/io/net_socket.cpp @@ -30,9 +30,9 @@ #include "net_socket.h" -NetSocket *(*NetSocket::_create)() = nullptr; +Ref (*NetSocket::_create)() = nullptr; -NetSocket *NetSocket::create() { +Ref NetSocket::create() { if (_create) { return _create(); } diff --git a/core/io/net_socket.h b/core/io/net_socket.h index 3ee3d284d1..40a2816927 100644 --- a/core/io/net_socket.h +++ b/core/io/net_socket.h @@ -37,10 +37,10 @@ class NetSocket : public RefCounted { GDSOFTCLASS(NetSocket, RefCounted); protected: - static NetSocket *(*_create)(); + static Ref (*_create)(); public: - static NetSocket *create(); + static Ref create(); enum PollType : int32_t { POLL_TYPE_IN, diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 0889810c3c..53c26b9f4a 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -750,7 +750,7 @@ Error ResourceLoaderBinary::load() { Ref res; Resource *r = nullptr; - MissingResource *missing_resource = nullptr; + Ref missing_resource; if (main) { res = ResourceLoader::get_resource_ref_override(local_path); @@ -776,7 +776,7 @@ Error ResourceLoaderBinary::load() { missing_resource = memnew(MissingResource); missing_resource->set_original_class(t); missing_resource->set_recording_properties(true); - obj = missing_resource; + obj = missing_resource.ptr(); } else { error = ERR_FILE_CORRUPT; ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, vformat("'%s': Resource of unrecognized type in file: '%s'.", local_path, t)); @@ -832,7 +832,7 @@ Error ResourceLoaderBinary::load() { } bool set_valid = true; - if (value.get_type() == Variant::OBJECT && missing_resource == nullptr && ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) { + if (value.get_type() == Variant::OBJECT && missing_resource.is_null() && ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) { // If the property being set is a missing resource (and the parent is not), // then setting it will most likely not work. // Instead, save it as metadata. @@ -874,7 +874,7 @@ Error ResourceLoaderBinary::load() { } } - if (missing_resource) { + if (missing_resource.is_valid()) { missing_resource->set_recording_properties(false); } diff --git a/core/io/udp_server.cpp b/core/io/udp_server.cpp index a2f1fc0778..a4a53b397a 100644 --- a/core/io/udp_server.cpp +++ b/core/io/udp_server.cpp @@ -149,7 +149,6 @@ void UDPServer::set_max_pending_connections(int p_max) { if (!E) { break; } - memdelete(E->get().peer); pending.erase(E); } } @@ -192,7 +191,6 @@ void UDPServer::stop() { E = pending.front(); while (E) { E->get().peer->disconnect_shared_socket(); - memdelete(E->get().peer); E = E->next(); } peers.clear(); diff --git a/core/io/udp_server.h b/core/io/udp_server.h index 9edf4318db..f2226b64e9 100644 --- a/core/io/udp_server.h +++ b/core/io/udp_server.h @@ -42,7 +42,7 @@ protected: }; struct Peer { - PacketPeerUDP *peer = nullptr; + Ref peer; IPAddress ip; uint16_t port = 0; diff --git a/core/math/static_raycaster.cpp b/core/math/static_raycaster.cpp index 9fa89a5f1e..619efe4567 100644 --- a/core/math/static_raycaster.cpp +++ b/core/math/static_raycaster.cpp @@ -30,7 +30,7 @@ #include "static_raycaster.h" -StaticRaycaster *(*StaticRaycaster::create_function)() = nullptr; +Ref (*StaticRaycaster::create_function)() = nullptr; Ref StaticRaycaster::create() { if (create_function) { diff --git a/core/math/static_raycaster.h b/core/math/static_raycaster.h index fbb5194cb1..e5d013cda1 100644 --- a/core/math/static_raycaster.h +++ b/core/math/static_raycaster.h @@ -35,7 +35,7 @@ class StaticRaycaster : public RefCounted { GDCLASS(StaticRaycaster, RefCounted) protected: - static StaticRaycaster *(*create_function)(); + static Ref (*create_function)(); public: // Compatible with embree4 rays. diff --git a/core/object/ref_counted.h b/core/object/ref_counted.h index fc12d955b1..258306b1c7 100644 --- a/core/object/ref_counted.h +++ b/core/object/ref_counted.h @@ -212,7 +212,9 @@ public: template void instantiate(VarArgs... p_params) { - ref(memnew(T(p_params...))); + Ref ref = memnew(T(p_params...)); + // Appropriate the new Ref, and if the previous one was set, free it. + SWAP(reference, ref.reference); } uint32_t hash() const { return HashMapHasherDefault::hash(reference); } @@ -224,6 +226,16 @@ public: } }; +template +struct memnew_result>> { + using class_name = Ref; +}; + +template +void postinitialize_handler(Ref &p_object) { + postinitialize_handler(p_object.ptr()); +} + class WeakRef : public RefCounted { GDCLASS(WeakRef, RefCounted); diff --git a/core/os/memory.h b/core/os/memory.h index 329c900341..602f8aae28 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -126,12 +126,21 @@ void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_d #define memrealloc(m_mem, m_size) Memory::realloc_static(m_mem, m_size) #define memfree(m_mem) Memory::free_static(m_mem) +template +struct memnew_result { + using class_name = T *; +}; + +template +using memnew_result_t = typename memnew_result::class_name; + _ALWAYS_INLINE_ void postinitialize_handler(void *) {} template -_ALWAYS_INLINE_ T *_post_initialize(T *p_obj) { - postinitialize_handler(p_obj); - return p_obj; +_ALWAYS_INLINE_ memnew_result_t _post_initialize(T *p_obj) { + memnew_result_t result{ p_obj }; + postinitialize_handler(result); + return result; } #define memnew(m_class) _post_initialize(::new ("") m_class) diff --git a/drivers/unix/net_socket_unix.cpp b/drivers/unix/net_socket_unix.cpp index 056ad77c18..01cd55776f 100644 --- a/drivers/unix/net_socket_unix.cpp +++ b/drivers/unix/net_socket_unix.cpp @@ -130,7 +130,7 @@ void NetSocketUnix::_set_ip_port(struct sockaddr_storage *p_addr, IPAddress *r_i } } -NetSocket *NetSocketUnix::_create_func() { +Ref NetSocketUnix::_create_func() { return memnew(NetSocketUnix); } @@ -796,7 +796,8 @@ Ref NetSocketUnix::_inet_accept(IPAddress &r_ip, uint16_t &r_port) { _set_ip_port(&their_addr, &r_ip, &r_port); - NetSocketUnix *ns = memnew(NetSocketUnix); + Ref ns; + ns.instantiate(); ns->_set_socket(fd, _ip_type, _is_stream); ns->set_blocking_enabled(false); return Ref(ns); @@ -813,7 +814,7 @@ Ref NetSocketUnix::_unix_accept() { return Ref(); } - NetSocketUnix *ret = memnew(NetSocketUnix); + Ref ret = memnew(NetSocketUnix); ret->_sock = fd; ret->_family = _family; ret->_unix_path = _unix_path; diff --git a/drivers/unix/net_socket_unix.h b/drivers/unix/net_socket_unix.h index 3f64420d38..1ef30d672e 100644 --- a/drivers/unix/net_socket_unix.h +++ b/drivers/unix/net_socket_unix.h @@ -65,7 +65,7 @@ private: _FORCE_INLINE_ void _set_close_exec_enabled(bool p_enabled); protected: - static NetSocket *_create_func(); + static Ref _create_func(); bool _can_use_ip(const IPAddress &p_ip, const bool p_for_bind) const; bool _can_use_path(const CharString &p_path) const; diff --git a/drivers/windows/net_socket_winsock.cpp b/drivers/windows/net_socket_winsock.cpp index b761fae3f3..065a6ad3ec 100644 --- a/drivers/windows/net_socket_winsock.cpp +++ b/drivers/windows/net_socket_winsock.cpp @@ -96,7 +96,7 @@ void NetSocketWinSock::_set_ip_port(struct sockaddr_storage *p_addr, IPAddress * } } -NetSocket *NetSocketWinSock::_create_func() { +Ref NetSocketWinSock::_create_func() { return memnew(NetSocketWinSock); } @@ -607,7 +607,7 @@ Ref NetSocketWinSock::accept(Address &r_addr) { _set_ip_port(&their_addr, &ip, &port); r_addr = Address(ip, port); - NetSocketWinSock *ns = memnew(NetSocketWinSock); + Ref ns = memnew(NetSocketWinSock); ns->_set_socket(fd, _ip_type, _is_stream); ns->set_blocking_enabled(false); return Ref(ns); diff --git a/drivers/windows/net_socket_winsock.h b/drivers/windows/net_socket_winsock.h index 3162e1ec64..a13cfef1ae 100644 --- a/drivers/windows/net_socket_winsock.h +++ b/drivers/windows/net_socket_winsock.h @@ -60,7 +60,7 @@ private: _FORCE_INLINE_ Error _change_multicast_group(IPAddress p_ip, String p_if_name, bool p_add); protected: - static NetSocket *_create_func(); + static Ref _create_func(); bool _can_use_ip(const IPAddress &p_ip, const bool p_for_bind) const; diff --git a/editor/animation/animation_blend_space_1d_editor.cpp b/editor/animation/animation_blend_space_1d_editor.cpp index 95998e893e..4082c6c059 100644 --- a/editor/animation/animation_blend_space_1d_editor.cpp +++ b/editor/animation/animation_blend_space_1d_editor.cpp @@ -817,7 +817,7 @@ void AnimationNodeBlendSpace1DEditor::_start_inline_edit(int p_point) { inline_editor->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("accent_color"), EditorStringName(Editor))); inline_editor->add_theme_color_override("font_selected_color", Color::named("white")); inline_editor->add_theme_color_override("selection_color", get_theme_color(SNAME("accent_color"), EditorStringName(Editor))); - StyleBoxEmpty *empty_style = memnew(StyleBoxEmpty); + Ref empty_style = memnew(StyleBoxEmpty); empty_style->set_content_margin_all(0); inline_editor->add_theme_style_override(CoreStringName(normal), empty_style); inline_editor->add_theme_style_override("focus", memnew(StyleBoxEmpty)); diff --git a/editor/animation/animation_blend_space_2d_editor.cpp b/editor/animation/animation_blend_space_2d_editor.cpp index 2310d93caf..f5f25b249d 100644 --- a/editor/animation/animation_blend_space_2d_editor.cpp +++ b/editor/animation/animation_blend_space_2d_editor.cpp @@ -1082,7 +1082,7 @@ void AnimationNodeBlendSpace2DEditor::_start_inline_edit(int p_point) { inline_editor->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("accent_color"), EditorStringName(Editor))); inline_editor->add_theme_color_override("font_selected_color", Color::named("white")); inline_editor->add_theme_color_override("selection_color", get_theme_color(SNAME("accent_color"), EditorStringName(Editor))); - StyleBoxEmpty *empty_style = memnew(StyleBoxEmpty); + Ref empty_style = memnew(StyleBoxEmpty); empty_style->set_content_margin_all(0); inline_editor->add_theme_style_override(CoreStringName(normal), empty_style); inline_editor->add_theme_style_override("focus", memnew(StyleBoxEmpty)); diff --git a/editor/animation/animation_player_editor_plugin.h b/editor/animation/animation_player_editor_plugin.h index 40ac7133c1..b178dd0f9a 100644 --- a/editor/animation/animation_player_editor_plugin.h +++ b/editor/animation/animation_player_editor_plugin.h @@ -331,7 +331,7 @@ public: class AnimationTrackKeyEditEditorPlugin : public EditorPlugin { GDCLASS(AnimationTrackKeyEditEditorPlugin, EditorPlugin); - EditorInspectorPluginAnimationTrackKeyEdit *atk_plugin = nullptr; + Ref atk_plugin; public: virtual bool handles(Object *p_object) const override; @@ -356,7 +356,7 @@ public: class AnimationMarkerKeyEditEditorPlugin : public EditorPlugin { GDCLASS(AnimationMarkerKeyEditEditorPlugin, EditorPlugin); - EditorInspectorPluginAnimationMarkerKeyEdit *amk_plugin = nullptr; + Ref amk_plugin; public: virtual bool handles(Object *p_object) const override; diff --git a/editor/debugger/editor_debugger_server.cpp b/editor/debugger/editor_debugger_server.cpp index a38bbd1fa4..1ae99a82eb 100644 --- a/editor/debugger/editor_debugger_server.cpp +++ b/editor/debugger/editor_debugger_server.cpp @@ -58,12 +58,12 @@ public: class EditorDebuggerServerTCP : public EditorDebuggerServerSocket { public: - static EditorDebuggerServer *create(const String &p_protocol); + static Ref create(const String &p_protocol); virtual Error start(const String &p_uri) override; }; -EditorDebuggerServer *EditorDebuggerServerTCP::create(const String &p_protocol) { +Ref EditorDebuggerServerTCP::create(const String &p_protocol) { ERR_FAIL_COND_V(p_protocol != "tcp://", nullptr); return memnew(EditorDebuggerServerTCP); } @@ -138,12 +138,12 @@ Ref EditorDebuggerServerSocket::take_connection() { class EditorDebuggerServerUDS : public EditorDebuggerServerSocket { public: - static EditorDebuggerServer *create(const String &p_protocol); + static Ref create(const String &p_protocol); virtual Error start(const String &p_uri) override; }; -EditorDebuggerServer *EditorDebuggerServerUDS::create(const String &p_protocol) { +Ref EditorDebuggerServerUDS::create(const String &p_protocol) { ERR_FAIL_COND_V(p_protocol != "unix://", nullptr); return memnew(EditorDebuggerServerUDS); } @@ -163,7 +163,7 @@ Error EditorDebuggerServerUDS::start(const String &p_uri) { /// EditorDebuggerServer HashMap EditorDebuggerServer::protocols; -EditorDebuggerServer *EditorDebuggerServer::create(const String &p_protocol) { +Ref EditorDebuggerServer::create(const String &p_protocol) { CreateServerFunc *create_fn = protocols.getptr(p_protocol); ERR_FAIL_NULL_V(create_fn, nullptr); return (*create_fn)(p_protocol); diff --git a/editor/debugger/editor_debugger_server.h b/editor/debugger/editor_debugger_server.h index 7d35a20a3a..b846da4470 100644 --- a/editor/debugger/editor_debugger_server.h +++ b/editor/debugger/editor_debugger_server.h @@ -37,7 +37,7 @@ class EditorDebuggerServer : public RefCounted { GDSOFTCLASS(EditorDebuggerServer, RefCounted); public: - typedef EditorDebuggerServer *(*CreateServerFunc)(const String &p_uri); + typedef Ref (*CreateServerFunc)(const String &p_uri); private: static HashMap protocols; @@ -47,7 +47,7 @@ public: static void deinitialize(); static void register_protocol_handler(const String &p_protocol, CreateServerFunc p_func); - static EditorDebuggerServer *create(const String &p_protocol); + static Ref create(const String &p_protocol); virtual String get_uri() const = 0; virtual void poll() = 0; diff --git a/editor/import/3d/resource_importer_scene.cpp b/editor/import/3d/resource_importer_scene.cpp index 1a227c7ab5..370a61b96b 100644 --- a/editor/import/3d/resource_importer_scene.cpp +++ b/editor/import/3d/resource_importer_scene.cpp @@ -827,19 +827,23 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, HashMap boxShape; + boxShape.instantiate(); boxShape->set_size(Vector3(2, 2, 2)); colshape->set_shape(boxShape); } else if (empty_draw_type == "SINGLE_ARROW") { - SeparationRayShape3D *rayShape = memnew(SeparationRayShape3D); + Ref rayShape; + rayShape.instantiate(); rayShape->set_length(1); colshape->set_shape(rayShape); Object::cast_to(sb)->rotate_x(Math::PI / 2); } else if (empty_draw_type == "IMAGE") { - WorldBoundaryShape3D *world_boundary_shape = memnew(WorldBoundaryShape3D); + Ref world_boundary_shape; + world_boundary_shape.instantiate(); colshape->set_shape(world_boundary_shape); } else { - SphereShape3D *sphereShape = memnew(SphereShape3D); + Ref sphereShape; + sphereShape.instantiate(); sphereShape->set_radius(1); colshape->set_shape(sphereShape); } diff --git a/editor/import/3d/scene_import_settings.cpp b/editor/import/3d/scene_import_settings.cpp index 3ab686cc05..53bfb6115f 100644 --- a/editor/import/3d/scene_import_settings.cpp +++ b/editor/import/3d/scene_import_settings.cpp @@ -79,7 +79,7 @@ class SceneImportSettingsData : public Object { SceneImportSettingsDialog::get_singleton()->request_generate_collider(); } - ResourceImporterScene *resource_importer_scene = SceneImportSettingsDialog::get_singleton()->get_resource_importer_scene(); + Ref resource_importer_scene = SceneImportSettingsDialog::get_singleton()->get_resource_importer_scene(); if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) { if (resource_importer_scene->get_option_visibility(path, p_name, current)) { SceneImportSettingsDialog::get_singleton()->update_view(); @@ -164,7 +164,7 @@ class SceneImportSettingsData : public Object { if (hide_options) { return; } - ResourceImporterScene *resource_importer_scene = SceneImportSettingsDialog::get_singleton()->get_resource_importer_scene(); + Ref resource_importer_scene = SceneImportSettingsDialog::get_singleton()->get_resource_importer_scene(); for (const ResourceImporter::ImportOption &E : options) { PropertyInfo option = E.option; if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) { @@ -2000,5 +2000,4 @@ SceneImportSettingsDialog::SceneImportSettingsDialog() { SceneImportSettingsDialog::~SceneImportSettingsDialog() { memdelete(scene_import_settings_data); - memdelete(_resource_importer_scene); } diff --git a/editor/import/3d/scene_import_settings.h b/editor/import/3d/scene_import_settings.h index 6aae7a9940..842c5c1c94 100644 --- a/editor/import/3d/scene_import_settings.h +++ b/editor/import/3d/scene_import_settings.h @@ -204,7 +204,7 @@ class SceneImportSettingsDialog : public ConfirmationDialog { HashMap defaults; SceneImportSettingsData *scene_import_settings_data = nullptr; - ResourceImporterScene *_resource_importer_scene = nullptr; + Ref _resource_importer_scene; void _re_import(); @@ -244,7 +244,7 @@ protected: void _notification(int p_what); public: - ResourceImporterScene *get_resource_importer_scene() const { return _resource_importer_scene; } + const Ref &get_resource_importer_scene() const { return _resource_importer_scene; } void request_generate_collider(); void update_view(); void open_settings(const String &p_path, const String &p_scene_import_type = "PackedScene"); diff --git a/editor/project_upgrade/project_converter_3_to_4.cpp b/editor/project_upgrade/project_converter_3_to_4.cpp index dd34c735ec..e9fb7d94a2 100644 --- a/editor/project_upgrade/project_converter_3_to_4.cpp +++ b/editor/project_upgrade/project_converter_3_to_4.cpp @@ -134,16 +134,16 @@ public: RegEx keyword_csharp_mastersync = RegEx("\\[MasterSync(Attribute)?(\\(\\))?\\]"); // Colors. - LocalVector color_regexes; + LocalVector> color_regexes; LocalVector color_renamed; RegEx color_hexadecimal_short_constructor = RegEx("Color\\(\"#?([a-fA-F0-9]{1})([a-fA-F0-9]{3})\\b"); RegEx color_hexadecimal_full_constructor = RegEx("Color\\(\"#?([a-fA-F0-9]{2})([a-fA-F0-9]{6})\\b"); // Classes. - LocalVector class_tscn_regexes; - LocalVector class_gd_regexes; - LocalVector class_shader_regexes; + LocalVector> class_tscn_regexes; + LocalVector> class_gd_regexes; + LocalVector> class_shader_regexes; // Keycode. RegEx input_map_keycode = RegEx("\\b,\"((physical_)?)scancode\":(\\d+)\\b"); @@ -160,7 +160,7 @@ public: // Animation suffixes. RegEx animation_suffix = RegEx("([\"'])([a-zA-Z0-9_-]+)(-(?:loop|cycle))([\"'])"); - LocalVector class_regexes; + LocalVector> class_regexes; RegEx class_temp_tscn = RegEx("\\bTEMP_RENAMED_CLASS.tscn\\b"); RegEx class_temp_gd = RegEx("\\bTEMP_RENAMED_CLASS.gd\\b"); @@ -171,19 +171,19 @@ public: LocalVector class_temp_shader_renames; // Common. - LocalVector enum_regexes; - LocalVector gdscript_function_regexes; - LocalVector project_settings_regexes; - LocalVector project_godot_regexes; - LocalVector input_map_regexes; - LocalVector gdscript_properties_regexes; - LocalVector gdscript_signals_regexes; - LocalVector shaders_regexes; - LocalVector builtin_types_regexes; - LocalVector theme_override_regexes; - LocalVector csharp_function_regexes; - LocalVector csharp_properties_regexes; - LocalVector csharp_signal_regexes; + LocalVector> enum_regexes; + LocalVector> gdscript_function_regexes; + LocalVector> project_settings_regexes; + LocalVector> project_godot_regexes; + LocalVector> input_map_regexes; + LocalVector> gdscript_properties_regexes; + LocalVector> gdscript_signals_regexes; + LocalVector> shaders_regexes; + LocalVector> builtin_types_regexes; + LocalVector> theme_override_regexes; + LocalVector> csharp_function_regexes; + LocalVector> csharp_properties_regexes; + LocalVector> csharp_signal_regexes; RegExContainer() { // Common. @@ -265,56 +265,6 @@ public: } } } - ~RegExContainer() { - for (RegEx *regex : color_regexes) { - memdelete(regex); - } - for (unsigned int i = 0; i < class_tscn_regexes.size(); i++) { - memdelete(class_tscn_regexes[i]); - memdelete(class_gd_regexes[i]); - memdelete(class_shader_regexes[i]); - memdelete(class_regexes[i]); - } - for (RegEx *regex : enum_regexes) { - memdelete(regex); - } - for (RegEx *regex : gdscript_function_regexes) { - memdelete(regex); - } - for (RegEx *regex : project_settings_regexes) { - memdelete(regex); - } - for (RegEx *regex : project_godot_regexes) { - memdelete(regex); - } - for (RegEx *regex : input_map_regexes) { - memdelete(regex); - } - for (RegEx *regex : gdscript_properties_regexes) { - memdelete(regex); - } - for (RegEx *regex : gdscript_signals_regexes) { - memdelete(regex); - } - for (RegEx *regex : shaders_regexes) { - memdelete(regex); - } - for (RegEx *regex : builtin_types_regexes) { - memdelete(regex); - } - for (RegEx *regex : theme_override_regexes) { - memdelete(regex); - } - for (RegEx *regex : csharp_function_regexes) { - memdelete(regex); - } - for (RegEx *regex : csharp_properties_regexes) { - memdelete(regex); - } - for (RegEx *regex : csharp_signal_regexes) { - memdelete(regex); - } - } }; ProjectConverter3To4::ProjectConverter3To4(int p_maximum_file_size_kb, int p_maximum_line_length) { @@ -772,7 +722,7 @@ bool ProjectConverter3To4::test_conversion_with_regex(const String &name, const return true; } -bool ProjectConverter3To4::test_conversion_basic(const String &name, const String &expected, const char *array[][2], LocalVector ®ex_cache, const String &what) { +bool ProjectConverter3To4::test_conversion_basic(const String &name, const String &expected, const char *array[][2], LocalVector> ®ex_cache, const String &what) { Vector got = split_lines(name); rename_common(array, regex_cache, got); @@ -2912,7 +2862,7 @@ Vector ProjectConverter3To4::check_for_custom_rename(Vector &lin return found_renames; } -void ProjectConverter3To4::rename_common(const char *array[][2], LocalVector &cached_regexes, Vector &source_lines) { +void ProjectConverter3To4::rename_common(const char *array[][2], LocalVector> &cached_regexes, Vector &source_lines) { for (SourceLine &source_line : source_lines) { if (source_line.is_comment) { continue; @@ -2929,7 +2879,7 @@ void ProjectConverter3To4::rename_common(const char *array[][2], LocalVector ProjectConverter3To4::check_for_rename_common(const char *array[][2], LocalVector &cached_regexes, Vector &lines) { +Vector ProjectConverter3To4::check_for_rename_common(const char *array[][2], LocalVector> &cached_regexes, Vector &lines) { Vector found_renames; int current_line = 1; diff --git a/editor/project_upgrade/project_converter_3_to_4.h b/editor/project_upgrade/project_converter_3_to_4.h index 0f7be9b61b..02b0b0fe25 100644 --- a/editor/project_upgrade/project_converter_3_to_4.h +++ b/editor/project_upgrade/project_converter_3_to_4.h @@ -42,6 +42,8 @@ struct SourceLine { }; class RegEx; +template +class Ref; class ProjectConverter3To4 { class RegExContainer; @@ -85,8 +87,8 @@ class ProjectConverter3To4 { void custom_rename(Vector &source_lines, const String &from, const String &to); Vector check_for_custom_rename(Vector &lines, const String &from, const String &to); - void rename_common(const char *array[][2], LocalVector &cached_regexes, Vector &source_lines); - Vector check_for_rename_common(const char *array[][2], LocalVector &cached_regexes, Vector &lines); + void rename_common(const char *array[][2], LocalVector> &cached_regexes, Vector &source_lines); + Vector check_for_rename_common(const char *array[][2], LocalVector> &cached_regexes, Vector &lines); Vector check_for_files(); @@ -105,7 +107,7 @@ class ProjectConverter3To4 { bool test_single_array(const char *array[][2], bool ignore_second_check = false); bool test_conversion_gdscript_builtin(const String &name, const String &expected, void (ProjectConverter3To4::*func)(Vector &, const RegExContainer &, bool), const String &what, const RegExContainer ®_container, bool builtin); bool test_conversion_with_regex(const String &name, const String &expected, void (ProjectConverter3To4::*func)(Vector &, const RegExContainer &), const String &what, const RegExContainer ®_container); - bool test_conversion_basic(const String &name, const String &expected, const char *array[][2], LocalVector ®ex_cache, const String &what); + bool test_conversion_basic(const String &name, const String &expected, const char *array[][2], LocalVector> ®ex_cache, const String &what); bool test_array_names(); bool test_conversion(RegExContainer ®_container); diff --git a/editor/scene/3d/node_3d_editor_plugin.cpp b/editor/scene/3d/node_3d_editor_plugin.cpp index b8be710c58..600f335715 100644 --- a/editor/scene/3d/node_3d_editor_plugin.cpp +++ b/editor/scene/3d/node_3d_editor_plugin.cpp @@ -6740,7 +6740,6 @@ Node3DEditorViewport::Node3DEditorViewport(Node3DEditor *p_spatial_editor, int p Node3DEditorViewport::~Node3DEditorViewport() { memdelete(ruler); - memdelete(frame_time_gradient); } ////////////////////////////////////////////////////////////// diff --git a/editor/scene/3d/node_3d_editor_plugin.h b/editor/scene/3d/node_3d_editor_plugin.h index 8a356d675b..bb06c9a163 100644 --- a/editor/scene/3d/node_3d_editor_plugin.h +++ b/editor/scene/3d/node_3d_editor_plugin.h @@ -259,7 +259,7 @@ private: ViewportNavigationControl *position_control = nullptr; ViewportNavigationControl *look_control = nullptr; ViewportRotationControl *rotation_control = nullptr; - Gradient *frame_time_gradient = nullptr; + Ref frame_time_gradient; PanelContainer *frame_time_panel = nullptr; VBoxContainer *frame_time_vbox = nullptr; Label *cpu_time_label = nullptr; diff --git a/editor/scene/3d/skeleton_3d_editor_plugin.cpp b/editor/scene/3d/skeleton_3d_editor_plugin.cpp index a73e62fd2c..bf63f6ebea 100644 --- a/editor/scene/3d/skeleton_3d_editor_plugin.cpp +++ b/editor/scene/3d/skeleton_3d_editor_plugin.cpp @@ -622,7 +622,8 @@ PhysicalBone3D *Skeleton3DEditor::create_physical_bone(int bone_id, int bone_chi const real_t half_height(child_rest.origin.length() * 0.5); const real_t radius(half_height * 0.2); - CapsuleShape3D *bone_shape_capsule = memnew(CapsuleShape3D); + Ref bone_shape_capsule; + bone_shape_capsule.instantiate(); bone_shape_capsule->set_height(half_height * 2); bone_shape_capsule->set_radius(radius); diff --git a/editor/scene/3d/skeleton_3d_editor_plugin.h b/editor/scene/3d/skeleton_3d_editor_plugin.h index c211a117cd..4a27c12017 100644 --- a/editor/scene/3d/skeleton_3d_editor_plugin.h +++ b/editor/scene/3d/skeleton_3d_editor_plugin.h @@ -268,7 +268,7 @@ public: class Skeleton3DEditorPlugin : public EditorPlugin { GDCLASS(Skeleton3DEditorPlugin, EditorPlugin); - EditorInspectorPluginSkeleton *skeleton_plugin = nullptr; + Ref skeleton_plugin; public: virtual EditorPlugin::AfterGUIInput forward_3d_gui_input(Camera3D *p_camera, const Ref &p_event) override; diff --git a/editor/script/script_editor_plugin.cpp b/editor/script/script_editor_plugin.cpp index b64240c677..cee5710da7 100644 --- a/editor/script/script_editor_plugin.cpp +++ b/editor/script/script_editor_plugin.cpp @@ -2126,8 +2126,8 @@ Ref ScriptEditor::_load_text_file(const String &p_path, Error *r_error String local_path = ProjectSettings::get_singleton()->localize_path(p_path); String path = ResourceLoader::path_remap(local_path); - TextFile *text_file = memnew(TextFile); - Ref text_res(text_file); + Ref text_file; + text_file.instantiate(); Error err = text_file->load_text(path); ERR_FAIL_COND_V_MSG(err != OK, Ref(), "Cannot load text file '" + path + "'."); @@ -2143,7 +2143,7 @@ Ref ScriptEditor::_load_text_file(const String &p_path, Error *r_error *r_error = OK; } - return text_res; + return text_file; } Error ScriptEditor::_save_text_file(Ref p_text_file, const String &p_path) { diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index e132c0c8c4..4fa88bb580 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -226,16 +226,17 @@ Variant GDScript::_new(const Variant **p_args, int p_argcount, Callable::CallErr ERR_FAIL_COND_V(_baseptr->native.is_null(), Variant()); if (_baseptr->native.ptr()) { owner = _baseptr->native->instantiate(); + + RefCounted *r = Object::cast_to(owner); + if (r) { + ref = Ref(r); + } } else { - owner = memnew(RefCounted); //by default, no base means use reference + ref = memnew(RefCounted); // By default, no base means use reference. + owner = ref.ptr(); } ERR_FAIL_NULL_V_MSG(owner, Variant(), "Can't inherit from a virtual class."); - RefCounted *r = Object::cast_to(owner); - if (r) { - ref = Ref(r); - } - GDScriptInstance *instance = _create_instance(p_args, p_argcount, owner, r_error); if (!instance) { if (ref.is_null()) { diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index 87067d6d1f..65a49231bd 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -4289,7 +4289,8 @@ void GLTFDocument::_convert_grid_map_to_gltf(GridMap *p_grid_map, GLTFNodeIndex #else const Array &cells = p_grid_map->get_used_cells(); for (int32_t k = 0; k < cells.size(); k++) { - GLTFNode *new_gltf_node = memnew(GLTFNode); + Ref new_gltf_node; + new_gltf_node.instantiate(); p_gltf_node->children.push_back(p_state->nodes.size()); p_state->nodes.push_back(new_gltf_node); Vector3 cell_location = cells[k]; diff --git a/modules/lightmapper_rd/register_types.cpp b/modules/lightmapper_rd/register_types.cpp index 738edc8554..80b36e33b6 100644 --- a/modules/lightmapper_rd/register_types.cpp +++ b/modules/lightmapper_rd/register_types.cpp @@ -37,7 +37,7 @@ #include "scene/3d/lightmapper.h" #ifndef _3D_DISABLED -static Lightmapper *create_lightmapper_rd() { +static Ref create_lightmapper_rd() { return memnew(LightmapperRD); } #endif diff --git a/modules/mbedtls/crypto_mbedtls.cpp b/modules/mbedtls/crypto_mbedtls.cpp index 784deecdda..b0483381c0 100644 --- a/modules/mbedtls/crypto_mbedtls.cpp +++ b/modules/mbedtls/crypto_mbedtls.cpp @@ -321,10 +321,7 @@ void CryptoMbedTLS::initialize_crypto() { void CryptoMbedTLS::finalize_crypto() { Crypto::_create = nullptr; Crypto::_load_default_certificates = nullptr; - if (default_certs) { - memdelete(default_certs); - default_certs = nullptr; - } + default_certs = nullptr; X509CertificateMbedTLS::finalize(); CryptoKeyMbedTLS::finalize(); HMACContextMbedTLS::finalize(); @@ -344,17 +341,17 @@ CryptoMbedTLS::~CryptoMbedTLS() { mbedtls_entropy_free(&entropy); } -X509CertificateMbedTLS *CryptoMbedTLS::default_certs = nullptr; +Ref CryptoMbedTLS::default_certs; -X509CertificateMbedTLS *CryptoMbedTLS::get_default_certificates() { +Ref CryptoMbedTLS::get_default_certificates() { return default_certs; } void CryptoMbedTLS::load_default_certificates(const String &p_path) { - ERR_FAIL_COND(default_certs != nullptr); + ERR_FAIL_COND(default_certs.is_valid()); default_certs = memnew(X509CertificateMbedTLS); - ERR_FAIL_NULL(default_certs); + ERR_FAIL_COND(default_certs.is_null()); if (!p_path.is_empty()) { // Use certs defined in project settings. diff --git a/modules/mbedtls/crypto_mbedtls.h b/modules/mbedtls/crypto_mbedtls.h index 2db47ebe29..803dd80593 100644 --- a/modules/mbedtls/crypto_mbedtls.h +++ b/modules/mbedtls/crypto_mbedtls.h @@ -132,13 +132,13 @@ class CryptoMbedTLS : public Crypto { private: mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; - static X509CertificateMbedTLS *default_certs; + static Ref default_certs; public: static Crypto *create(bool p_notify_postinitialize = true); static void initialize_crypto(); static void finalize_crypto(); - static X509CertificateMbedTLS *get_default_certificates(); + static Ref get_default_certificates(); static void load_default_certificates(const String &p_path); static mbedtls_md_type_t md_type_from_hashtype(HashingContext::HashType p_hash_type, int &r_size); diff --git a/modules/mbedtls/tls_context_mbedtls.cpp b/modules/mbedtls/tls_context_mbedtls.cpp index 6895639146..9c791c50d4 100644 --- a/modules/mbedtls/tls_context_mbedtls.cpp +++ b/modules/mbedtls/tls_context_mbedtls.cpp @@ -193,17 +193,17 @@ Error TLSContextMbedTLS::init_client(int p_transport, const String &p_hostname, mbedtls_ssl_set_hostname(&tls, cn.utf8().get_data()); } - X509CertificateMbedTLS *cas = nullptr; + Ref cas; if (p_options->get_trusted_ca_chain().is_valid()) { // Locking CA certificates certs = p_options->get_trusted_ca_chain(); certs->lock(); - cas = certs.ptr(); + cas = certs; } else { // Fall back to default certificates (no need to lock those). cas = CryptoMbedTLS::get_default_certificates(); - if (cas == nullptr) { + if (cas.is_null()) { clear(); ERR_FAIL_V_MSG(ERR_UNCONFIGURED, "SSL module failed to initialize!"); } diff --git a/modules/raycast/lightmap_raycaster_embree.cpp b/modules/raycast/lightmap_raycaster_embree.cpp index 84d9e19a3f..eb8fd34f83 100644 --- a/modules/raycast/lightmap_raycaster_embree.cpp +++ b/modules/raycast/lightmap_raycaster_embree.cpp @@ -36,7 +36,7 @@ #include #endif -LightmapRaycaster *LightmapRaycasterEmbree::create_embree_raycaster() { +Ref LightmapRaycasterEmbree::create_embree_raycaster() { return memnew(LightmapRaycasterEmbree); } diff --git a/modules/raycast/lightmap_raycaster_embree.h b/modules/raycast/lightmap_raycaster_embree.h index 3335e943f6..f561aa9117 100644 --- a/modules/raycast/lightmap_raycaster_embree.h +++ b/modules/raycast/lightmap_raycaster_embree.h @@ -69,7 +69,7 @@ public: virtual void set_mesh_filter(const HashSet &p_mesh_ids) override; virtual void clear_mesh_filter() override; - static LightmapRaycaster *create_embree_raycaster(); + static Ref create_embree_raycaster(); static void make_default_raycaster(); LightmapRaycasterEmbree(); diff --git a/modules/raycast/static_raycaster_embree.cpp b/modules/raycast/static_raycaster_embree.cpp index a6ad340397..aca3063330 100644 --- a/modules/raycast/static_raycaster_embree.cpp +++ b/modules/raycast/static_raycaster_embree.cpp @@ -38,7 +38,7 @@ RTCDevice StaticRaycasterEmbree::embree_device; -StaticRaycaster *StaticRaycasterEmbree::create_embree_raycaster() { +Ref StaticRaycasterEmbree::create_embree_raycaster() { return memnew(StaticRaycasterEmbree); } diff --git a/modules/raycast/static_raycaster_embree.h b/modules/raycast/static_raycaster_embree.h index a0b66b5c1e..410a23520e 100644 --- a/modules/raycast/static_raycaster_embree.h +++ b/modules/raycast/static_raycaster_embree.h @@ -55,7 +55,7 @@ public: virtual void set_mesh_filter(const HashSet &p_mesh_ids) override; virtual void clear_mesh_filter() override; - static StaticRaycaster *create_embree_raycaster(); + static Ref create_embree_raycaster(); static void make_default_raycaster(); static void free(); diff --git a/modules/theora/video_stream_theora.cpp b/modules/theora/video_stream_theora.cpp index e25b43adad..8a219dc85a 100644 --- a/modules/theora/video_stream_theora.cpp +++ b/modules/theora/video_stream_theora.cpp @@ -799,7 +799,8 @@ Ref ResourceFormatLoaderTheora::load(const String &p_path, const Strin return Ref(); } - VideoStreamTheora *stream = memnew(VideoStreamTheora); + Ref stream; + stream.instantiate(); stream->set_file(p_path); Ref ogv_stream = Ref(stream); diff --git a/modules/websocket/editor/editor_debugger_server_websocket.cpp b/modules/websocket/editor/editor_debugger_server_websocket.cpp index b859a915d8..6819063b66 100644 --- a/modules/websocket/editor/editor_debugger_server_websocket.cpp +++ b/modules/websocket/editor/editor_debugger_server_websocket.cpp @@ -117,7 +117,7 @@ bool EditorDebuggerServerWebSocket::is_connection_available() const { Ref EditorDebuggerServerWebSocket::take_connection() { ERR_FAIL_COND_V(!is_connection_available(), Ref()); - RemoteDebuggerPeer *peer = memnew(RemoteDebuggerPeerWebSocket(pending_peer)); + Ref peer = memnew(RemoteDebuggerPeerWebSocket(pending_peer)); pending_peer.unref(); return peer; } @@ -130,7 +130,7 @@ EditorDebuggerServerWebSocket::~EditorDebuggerServerWebSocket() { stop(); } -EditorDebuggerServer *EditorDebuggerServerWebSocket::create(const String &p_protocol) { +Ref EditorDebuggerServerWebSocket::create(const String &p_protocol) { ERR_FAIL_COND_V(p_protocol != "ws://", nullptr); return memnew(EditorDebuggerServerWebSocket); } diff --git a/modules/websocket/editor/editor_debugger_server_websocket.h b/modules/websocket/editor/editor_debugger_server_websocket.h index 09f0436fb2..e3aa038c7c 100644 --- a/modules/websocket/editor/editor_debugger_server_websocket.h +++ b/modules/websocket/editor/editor_debugger_server_websocket.h @@ -45,7 +45,7 @@ private: String endpoint; public: - static EditorDebuggerServer *create(const String &p_protocol); + static Ref create(const String &p_protocol); void _peer_connected(int p_peer, const String &p_protocol); void _peer_disconnected(int p_peer, bool p_was_clean); diff --git a/modules/websocket/remote_debugger_peer_websocket.cpp b/modules/websocket/remote_debugger_peer_websocket.cpp index 5102f402b9..b555ee6689 100644 --- a/modules/websocket/remote_debugger_peer_websocket.cpp +++ b/modules/websocket/remote_debugger_peer_websocket.cpp @@ -129,12 +129,12 @@ RemoteDebuggerPeerWebSocket::RemoteDebuggerPeerWebSocket(const Ref RemoteDebuggerPeerWebSocket::create(const String &p_uri) { ERR_FAIL_COND_V(!p_uri.begins_with("ws://") && !p_uri.begins_with("wss://"), nullptr); - RemoteDebuggerPeerWebSocket *peer = memnew(RemoteDebuggerPeerWebSocket); + Ref peer; + peer.instantiate(); Error err = peer->connect_to_host(p_uri); if (err != OK) { - memdelete(peer); return nullptr; } return peer; diff --git a/modules/websocket/remote_debugger_peer_websocket.h b/modules/websocket/remote_debugger_peer_websocket.h index 149fa69739..b26144c07a 100644 --- a/modules/websocket/remote_debugger_peer_websocket.h +++ b/modules/websocket/remote_debugger_peer_websocket.h @@ -44,7 +44,7 @@ class RemoteDebuggerPeerWebSocket : public RemoteDebuggerPeer { int max_queued_messages; public: - static RemoteDebuggerPeer *create(const String &p_uri); + static Ref create(const String &p_uri); Error connect_to_host(const String &p_uri); diff --git a/platform/android/net_socket_android.cpp b/platform/android/net_socket_android.cpp index 9ab7d6a04f..2fda21977e 100644 --- a/platform/android/net_socket_android.cpp +++ b/platform/android/net_socket_android.cpp @@ -71,7 +71,7 @@ void NetSocketAndroid::multicast_lock_release() { } } -NetSocket *NetSocketAndroid::_create_func() { +Ref NetSocketAndroid::_create_func() { return memnew(NetSocketAndroid); } diff --git a/platform/android/net_socket_android.h b/platform/android/net_socket_android.h index 21a3f6a3ec..9aea9278f0 100644 --- a/platform/android/net_socket_android.h +++ b/platform/android/net_socket_android.h @@ -59,7 +59,7 @@ private: static void multicast_lock_release(); protected: - static NetSocket *_create_func(); + static Ref _create_func(); public: static void make_default(); diff --git a/platform/linuxbsd/wayland/wayland_thread.cpp b/platform/linuxbsd/wayland/wayland_thread.cpp index 46b89d76f3..2898ce0c41 100644 --- a/platform/linuxbsd/wayland/wayland_thread.cpp +++ b/platform/linuxbsd/wayland/wayland_thread.cpp @@ -2700,11 +2700,11 @@ void WaylandThread::_wp_image_description_on_ready2(void *data, struct wp_image_ struct wp_image_description_info_v1 *image_info = wp_image_description_v1_get_information(image_descriptor); if (image_info != nullptr) { - ColorProfileMessage *msg = memnew(ColorProfileMessage); + Ref msg = memnew(ColorProfileMessage); msg->id = ws->id; msg->wayland_thread = ws->wayland_thread; - wp_image_description_info_v1_add_listener(image_info, &wp_image_description_info_listener, msg); + wp_image_description_info_v1_add_listener(image_info, &wp_image_description_info_listener, msg.ptr()); wp_image_description_v1_destroy(image_descriptor); } } diff --git a/platform/web/net_socket_web.h b/platform/web/net_socket_web.h index f993b6b2e8..dfc0c34237 100644 --- a/platform/web/net_socket_web.h +++ b/platform/web/net_socket_web.h @@ -38,7 +38,7 @@ class NetSocketWeb : public NetSocket { GDSOFTCLASS(NetSocketWeb, NetSocket); protected: - static NetSocket *_create_func() { + static Ref _create_func() { return memnew(NetSocketWeb); } diff --git a/scene/3d/lightmapper.cpp b/scene/3d/lightmapper.cpp index 0a71a73094..1e1571bc5c 100644 --- a/scene/3d/lightmapper.cpp +++ b/scene/3d/lightmapper.cpp @@ -30,7 +30,7 @@ #include "lightmapper.h" -LightmapDenoiser *(*LightmapDenoiser::create_function)() = nullptr; +Ref (*LightmapDenoiser::create_function)() = nullptr; Ref LightmapDenoiser::create() { if (create_function) { @@ -39,7 +39,7 @@ Ref LightmapDenoiser::create() { return Ref(); } -LightmapRaycaster *(*LightmapRaycaster::create_function)() = nullptr; +Ref (*LightmapRaycaster::create_function)() = nullptr; Ref LightmapRaycaster::create() { if (create_function) { @@ -53,19 +53,19 @@ Lightmapper::CreateFunc Lightmapper::create_gpu = nullptr; Lightmapper::CreateFunc Lightmapper::create_cpu = nullptr; Ref Lightmapper::create() { - Lightmapper *lm = nullptr; + Ref lm; if (create_custom) { lm = create_custom(); } - if (!lm && create_gpu) { + if (lm.is_null() && create_gpu) { lm = create_gpu(); } - if (!lm && create_cpu) { + if (lm.is_null() && create_cpu) { lm = create_cpu(); } - if (!lm) { + if (lm.is_null()) { return Ref(); } else { return Ref(lm); diff --git a/scene/3d/lightmapper.h b/scene/3d/lightmapper.h index 99c1edc82a..3bc04454a5 100644 --- a/scene/3d/lightmapper.h +++ b/scene/3d/lightmapper.h @@ -37,7 +37,7 @@ class Image; class LightmapDenoiser : public RefCounted { GDCLASS(LightmapDenoiser, RefCounted) protected: - static LightmapDenoiser *(*create_function)(); + static Ref (*create_function)(); public: virtual Ref denoise_image(const Ref &p_image) = 0; @@ -47,7 +47,7 @@ public: class LightmapRaycaster : public RefCounted { GDCLASS(LightmapRaycaster, RefCounted) protected: - static LightmapRaycaster *(*create_function)(); + static Ref (*create_function)(); public: // Compatible with embree4 rays. @@ -145,7 +145,7 @@ public: BAKE_QUALITY_ULTRA, }; - typedef Lightmapper *(*CreateFunc)(); + typedef Ref (*CreateFunc)(); static CreateFunc create_custom; static CreateFunc create_gpu; diff --git a/scene/resources/2d/tile_set.cpp b/scene/resources/2d/tile_set.cpp index ab748e5195..612ef8ff8d 100644 --- a/scene/resources/2d/tile_set.cpp +++ b/scene/resources/2d/tile_set.cpp @@ -3355,8 +3355,9 @@ void TileSet::_compatibility_conversion() { CompatibilityTileData *ctd = E.value; // Add the texture - TileSetAtlasSource *atlas_source = memnew(TileSetAtlasSource); - int source_id = add_source(Ref(atlas_source)); + Ref atlas_source; + atlas_source.instantiate(); + int source_id = add_source(atlas_source); atlas_source->set_texture(ctd->texture); diff --git a/scene/resources/resource_format_text.cpp b/scene/resources/resource_format_text.cpp index 321292c6a0..f9857c24c8 100644 --- a/scene/resources/resource_format_text.cpp +++ b/scene/resources/resource_format_text.cpp @@ -583,7 +583,7 @@ Error ResourceLoaderText::load() { } } - MissingResource *missing_resource = nullptr; + Ref missing_resource; if (res.is_null()) { //not reuse Ref cache = ResourceCache::get_ref(path); @@ -599,7 +599,7 @@ Error ResourceLoaderText::load() { missing_resource = memnew(MissingResource); missing_resource->set_original_class(type); missing_resource->set_recording_properties(true); - obj = missing_resource; + obj = missing_resource.ptr(); } else { error_text = vformat("Can't create sub resource of type '%s'", type); _printerr(); @@ -654,7 +654,7 @@ Error ResourceLoaderText::load() { if (do_assign) { bool set_valid = true; - if (value.get_type() == Variant::OBJECT && missing_resource == nullptr && ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) { + if (value.get_type() == Variant::OBJECT && missing_resource.is_null() && ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) { // If the property being set is a missing resource (and the parent is not), // then setting it will most likely not work. // Instead, save it as metadata. @@ -707,7 +707,7 @@ Error ResourceLoaderText::load() { } } - if (missing_resource) { + if (missing_resource.is_valid()) { missing_resource->set_recording_properties(false); } @@ -728,7 +728,7 @@ Error ResourceLoaderText::load() { return error; } - MissingResource *missing_resource = nullptr; + Ref missing_resource; resource = ResourceLoader::get_resource_ref_override(local_path); if (resource.is_null()) { @@ -745,7 +745,7 @@ Error ResourceLoaderText::load() { missing_resource = memnew(MissingResource); missing_resource->set_original_class(res_type); missing_resource->set_recording_properties(true); - obj = missing_resource; + obj = missing_resource.ptr(); } else { error_text = vformat("Can't create sub resource of type '%s'", res_type); _printerr(); @@ -795,7 +795,7 @@ Error ResourceLoaderText::load() { if (!assign.is_empty()) { bool set_valid = true; - if (value.get_type() == Variant::OBJECT && missing_resource == nullptr && ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) { + if (value.get_type() == Variant::OBJECT && missing_resource.is_null() && ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) { // If the property being set is a missing resource (and the parent is not), // then setting it will most likely not work. // Instead, save it as metadata. @@ -852,7 +852,7 @@ Error ResourceLoaderText::load() { *progress = resource_current / float(resources_total); } - if (missing_resource) { + if (missing_resource.is_valid()) { missing_resource->set_recording_properties(false); } diff --git a/tests/core/io/test_resource.cpp b/tests/core/io/test_resource.cpp index 2b95f7e943..44e25f3602 100644 --- a/tests/core/io/test_resource.cpp +++ b/tests/core/io/test_resource.cpp @@ -350,7 +350,7 @@ public: } \ \ public: \ - static m_class_name *register_and_instantiate() { \ + static Ref register_and_instantiate() { \ static bool registered = false; \ if (!registered) { \ GDREGISTER_CLASS(m_class_name); \ diff --git a/tests/core/io/test_stream_peer_tcp.cpp b/tests/core/io/test_stream_peer_tcp.cpp index df17461cda..b4e5b574ab 100644 --- a/tests/core/io/test_stream_peer_tcp.cpp +++ b/tests/core/io/test_stream_peer_tcp.cpp @@ -81,7 +81,7 @@ public: ~MockNetSocket() override; protected: - static NetSocket *_create_func(); + static Ref _create_func(); private: bool _is_open = false; @@ -90,7 +90,7 @@ private: uint8_t *_recv_data = nullptr; }; -NetSocket *MockNetSocket::_create_func() { +Ref MockNetSocket::_create_func() { return memnew(MockNetSocket); } diff --git a/tests/scene/test_packed_scene.cpp b/tests/scene/test_packed_scene.cpp index 48e3b53d41..ac91dffe53 100644 --- a/tests/scene/test_packed_scene.cpp +++ b/tests/scene/test_packed_scene.cpp @@ -267,14 +267,15 @@ TEST_CASE("[PackedScene] Recreate State") { scene->set_name("TestScene"); // Pack the scene. - PackedScene packed_scene; - packed_scene.pack(scene); + Ref packed_scene; + packed_scene.instantiate(); + packed_scene->pack(scene); // Recreate the state. - packed_scene.recreate_state(); + packed_scene->recreate_state(); // Check if the state has been recreated. - Ref state = packed_scene.get_state(); + Ref state = packed_scene->get_state(); CHECK(state.is_valid()); CHECK(state->get_node_count() == 0); // Since the state was recreated, it should be empty.