| 2262 | } |
| 2263 | |
| 2264 | Object::~Object() { |
| 2265 | if (script_instance) { |
| 2266 | memdelete(script_instance); |
| 2267 | } |
| 2268 | script_instance = nullptr; |
| 2269 | |
| 2270 | if (_extension) { |
| 2271 | #ifdef TOOLS_ENABLED |
| 2272 | if (_extension->untrack_instance) { |
| 2273 | _extension->untrack_instance(_extension->tracking_userdata, this); |
| 2274 | } |
| 2275 | #endif |
| 2276 | if (_extension->free_instance) { |
| 2277 | _extension->free_instance(_extension->class_userdata, _extension_instance); |
| 2278 | } |
| 2279 | _extension = nullptr; |
| 2280 | _extension_instance = nullptr; |
| 2281 | } |
| 2282 | #ifdef TOOLS_ENABLED |
| 2283 | else if (_instance_bindings != nullptr) { |
| 2284 | Engine *engine = Engine::get_singleton(); |
| 2285 | GDExtensionManager *gdextension_manager = GDExtensionManager::get_singleton(); |
| 2286 | if (engine && gdextension_manager && engine->is_extension_reloading_enabled()) { |
| 2287 | for (uint32_t i = 0; i < _instance_binding_count; i++) { |
| 2288 | gdextension_manager->untrack_instance_binding(_instance_bindings[i].token, this); |
| 2289 | } |
| 2290 | } |
| 2291 | } |
| 2292 | #endif |
| 2293 | |
| 2294 | if (_emitting) { |
| 2295 | /// @todo This may need to actually reach the debugger prioritarily somehow because it may crash before |
| 2296 | ERR_PRINT(vformat("Object '%s' was freed or unreferenced while a signal is being emitted from it. Try connecting to the signal using 'CONNECT_DEFERRED' flag, or use queue_free() to free the object (if this object is a Node) to avoid this error and potential crashes.", to_string())); |
| 2297 | } |
| 2298 | |
| 2299 | { |
| 2300 | OBJ_SIGNAL_LOCK |
| 2301 | // Drop all connections to the signals of this object. |
| 2302 | while (signal_map.size()) { |
| 2303 | // Avoid regular iteration so erasing is safe. |
| 2304 | KeyValue<StringName, SignalData> &E = *signal_map.begin(); |
| 2305 | SignalData *s = &E.value; |
| 2306 | |
| 2307 | for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) { |
| 2308 | Object *target = slot_kv.value.conn.callable.get_object(); |
| 2309 | if (likely(target)) { |
| 2310 | target->connections.erase(slot_kv.value.cE); |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | signal_map.erase(E.key); |
| 2315 | } |
| 2316 | |
| 2317 | // Disconnect signals that connect to this object. |
| 2318 | while (connections.size()) { |
| 2319 | Connection c = connections.front()->get(); |
| 2320 | Object *obj = c.callable.get_object(); |
| 2321 | bool disconnected = false; |