diff --git a/core/debugger/engine_debugger.cpp b/core/debugger/engine_debugger.cpp index ee0a06a0c34c..c51903108f07 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 95b64458f6f4..80deb1f46a8a 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 @@ class EngineDebugger { 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 759394a2a541..3bb7b4d398a4 100644 --- a/core/debugger/remote_debugger_peer.cpp +++ b/core/debugger/remote_debugger_peer.cpp @@ -208,7 +208,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://", ""); @@ -234,7 +234,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 6942c09bf96e..8105506f7825 100644 --- a/core/debugger/remote_debugger_peer.h +++ b/core/debugger/remote_debugger_peer.h @@ -82,8 +82,8 @@ class RemoteDebuggerPeerTCP : public RemoteDebuggerPeer { 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 f7a2c5e38ce8..e5bdc4d1b9b8 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 3ee3d284d11b..40a281692781 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 7ce92989f7f7..331401f0b589 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -749,7 +749,7 @@ Error ResourceLoaderBinary::load() { Ref res; Resource *r = nullptr; - MissingResource *missing_resource = nullptr; + Ref missing_resource = nullptr; if (main) { res = ResourceLoader::get_resource_ref_override(local_path); @@ -775,7 +775,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)); @@ -831,7 +831,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. @@ -873,7 +873,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 ada0e09a48f7..cf2652e3e8ab 100644 --- a/core/io/udp_server.cpp +++ b/core/io/udp_server.cpp @@ -147,7 +147,6 @@ void UDPServer::set_max_pending_connections(int p_max) { if (!E) { break; } - memdelete(E->get().peer); pending.erase(E); } } @@ -190,7 +189,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 9edf4318db28..f2226b64e9ef 100644 --- a/core/io/udp_server.h +++ b/core/io/udp_server.h @@ -42,7 +42,7 @@ class UDPServer : public RefCounted { }; 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 9fa89a5f1ead..619efe456781 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 fbb5194cb1ee..e5d013cda198 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 9b6c3c7a85b5..e5fdf10ad1ac 100644 --- a/core/object/ref_counted.h +++ b/core/object/ref_counted.h @@ -213,7 +213,10 @@ class Ref { template void instantiate(VarArgs... p_params) { - ref(memnew(T(p_params...))); + Ref ref = memnew(T(p_params...)); + // Appropriate the new Ref. + reference = ref.reference; + ref.reference = nullptr; } uint32_t hash() const { return HashMapHasherDefault::hash(reference); } @@ -225,6 +228,16 @@ class Ref { } }; +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/object/script_language.h b/core/object/script_language.h index e4c6427e7d12..a892a709e04f 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -272,7 +272,7 @@ class ScriptLanguage : public Object { virtual bool is_using_templates() { return false; } virtual bool validate(const String &p_script, const String &p_path = "", List *r_functions = nullptr, List *r_errors = nullptr, List *r_warnings = nullptr, HashSet *r_safe_lines = nullptr) const = 0; virtual String validate_path(const String &p_path) const { return ""; } - virtual Script *create_script() const = 0; + virtual Ref