diff --git a/Plugins/UnLua/Source/UnLua/Private/Registries/DelegateRegistry.cpp b/Plugins/UnLua/Source/UnLua/Private/Registries/DelegateRegistry.cpp index aa59a3433..f9292cf30 100644 --- a/Plugins/UnLua/Source/UnLua/Private/Registries/DelegateRegistry.cpp +++ b/Plugins/UnLua/Source/UnLua/Private/Registries/DelegateRegistry.cpp @@ -41,29 +41,43 @@ namespace UnLua void FDelegateRegistry::OnPostGarbageCollect() { + TSet InvalidDelegates; TArray> InvalidPairs; for (auto& Pair : Delegates) { if (!Pair.Value.Owner.IsValid()) + { InvalidPairs.Add(Pair); + InvalidDelegates.Add(Pair.Key); + } } for (int i = 0; i < InvalidPairs.Num(); i++) { const auto& Pair = InvalidPairs[i]; + if (Pair.Value.bIsMulticast) + { Clear(Pair.Key); + } else + { Unbind(Pair.Key); + } Delegates.Remove(Pair.Key); + if (Pair.Value.bDeleteOnRemove) + { delete (FScriptDelegate*)Pair.Key; + } } TArray ToRemove; for (auto& Pair : CachedHandlers) { - if (Pair.Key.SelfObject.IsStale()) + if (Pair.Key.SelfObject.IsStale() + || !Pair.Value.IsValid() + || InvalidDelegates.Contains(Pair.Value->Delegate)) { ToRemove.Add(Pair.Key); Env->AutoObjectReference.Remove(Pair.Value.Get());