I’m working on using events to set up a cursory observer pattern, and let’s say I do something like the following:
public delegate void SomeNotification(GameObject invokingObject, ObserverEvent eventEnum);
public class SubjectClass : MonoBehaviour
{
public event SomeNotification testNotification;
}
public class ObservingClass : MonoBehaviour {
SubjectClass testSubject;
public void OnNotify(GameObject invokingObject, ObserverEvent eventEnum)
{
}
void Awake()
{
testSubject = new SubjectClass();
testSubject.testNotification += OnNotify;
Destroy(testSubject);
}
}
I’ll never get a null reference, because SubjectClass was the one subscribed to ObservingClass’s OnNotify, not vice-versa. However, I never explicitly un-subscribed testNotification from the things it was talking to before destroying its object. Can I trust Unity to clean this up after the Destroy call, or have I just created an inadvertent memory leak by deleting SubjectClass before calling testNotification -=OnNotify?