Skip to content

Commit

Permalink
Merge pull request #47019 from neikeq/csharp-fix-disconnec-events-sig…
Browse files Browse the repository at this point in the history
…nals-twice

C#: Fix disconnecting event signals twice
  • Loading branch information
akien-mga authored Mar 15, 2021
2 parents 5375da2 + 5b3b9cd commit d1b5c1d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 9 additions & 11 deletions modules/mono/csharp_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2018,22 +2018,20 @@ void CSharpInstance::connect_event_signals() {
// TODO: Use pooling for ManagedCallable instances.
auto event_signal_callable = memnew(EventSignalCallable(owner, &event_signal));

owner->connect(signal_name, Callable(event_signal_callable));
Callable callable(event_signal_callable);
connected_event_signals.push_back(callable);
owner->connect(signal_name, callable);
}
}

void CSharpInstance::disconnect_event_signals() {
for (const Map<StringName, CSharpScript::EventSignal>::Element *E = script->event_signals.front(); E; E = E->next()) {
const CSharpScript::EventSignal &event_signal = E->value();

StringName signal_name = event_signal.field->get_name();

// TODO: It would be great if we could store this EventSignalCallable on the stack.
// The problem is that Callable memdeletes it when it's destructed...
auto event_signal_callable = memnew(EventSignalCallable(owner, &event_signal));

owner->disconnect(signal_name, Callable(event_signal_callable));
for (const List<Callable>::Element *E = connected_event_signals.front(); E; E = E->next()) {
const Callable &callable = E->get();
auto event_signal_callable = static_cast<const EventSignalCallable *>(callable.get_custom());
owner->disconnect(event_signal_callable->get_signal(), callable);
}

connected_event_signals.clear();
}

void CSharpInstance::refcount_incremented() {
Expand Down
2 changes: 2 additions & 0 deletions modules/mono/csharp_script.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ class CSharpInstance : public ScriptInstance {
Ref<CSharpScript> script;
MonoGCHandleData gchandle;

List<Callable> connected_event_signals;

bool _reference_owner_unsafe();

/*
Expand Down

0 comments on commit d1b5c1d

Please sign in to comment.