Skip to content

Commit c67c987

Browse files
committed
Fix regression in mechanism to hold objects while emitting
1 parent 8ebf8ae commit c67c987

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

core/object/object.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,13 @@ Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int
12211221
uint32_t *slot_flags = slot_flags_stack;
12221222
uint32_t slot_count = 0;
12231223

1224+
// If this is a ref-counted object, prevent it from being destroyed during signal
1225+
// emission,which is needed in certain edge cases; e.g., GH-73889 and GH-109471.
1226+
// Moreover, since signals can be emitted from constructors (classic example being
1227+
// notify_property_list_changed), we must be careful not to do the ref init ourselves,
1228+
// which would lead to the object being destroyed at the end of this function.
1229+
bool pending_unref = Object::cast_to<RefCounted>(this) ? ((RefCounted *)this)->reference() : false;
1230+
12241231
{
12251232
OBJ_SIGNAL_LOCK
12261233

@@ -1235,10 +1242,6 @@ Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int
12351242
return ERR_UNAVAILABLE;
12361243
}
12371244

1238-
// If this is a ref-counted object, prevent it from being destroyed during signal emission,
1239-
// which is needed in certain edge cases; e.g., https://github.com/godotengine/godot/issues/73889.
1240-
Ref<RefCounted> rc = Ref<RefCounted>(Object::cast_to<RefCounted>(this));
1241-
12421245
if (s->slot_map.size() > MAX_SLOTS_ON_STACK) {
12431246
slot_callables = (Callable *)memalloc(sizeof(Callable) * s->slot_map.size());
12441247
slot_flags = (uint32_t *)memalloc(sizeof(uint32_t) * s->slot_map.size());
@@ -1320,6 +1323,15 @@ Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int
13201323
memfree(slot_flags);
13211324
}
13221325

1326+
if (pending_unref) {
1327+
// We have to do the same RefCounted would do. We can't just use RefCounted
1328+
// because it would do the init ref logic, which is something this function
1329+
// shouldn't do, as explained above.
1330+
if (((RefCounted *)this)->unreference()) {
1331+
memdelete(this);
1332+
}
1333+
}
1334+
13231335
return err;
13241336
}
13251337

0 commit comments

Comments
 (0)