Hello to all!

The situation is as follows: created a class object, in it subscribed to events from the outside. Now, if I delete the subscriber, will it automatically be deleted in OnSceneChanged? Or should it be unsubscribed first, and then deleted?

public class PassButton : Emitter { public delegate void OnSceneChangedHandler(); public event OnSceneChangedHandler OnSceneChanged; public void SceneChanged(){// called from (scene changed) OnSceneChanged ();// Awake subscripters } ... 
  • 2
    Something you wondered. This is a typical memory leak. In general, if you subscribe to someone, this someone will have a link to you through the delegate and unless you unsubscribe, the garbage collector obviously does not collect you. Because you need to either unsubscribe explicitly or subscribe weak links (which is awkwardly awkwardly implemented) - vitidev
  • Looks like you didn't understand me. See, subscribed to events. Then this object was deleted (the subscriber was deleted). That is, the link to it in OnSceneChanged has been nullified? Or is it necessary to unsubscribe first, and then not to delete the subscriber? - Alerr
  • Where did they remove the subscriber? What is this "deleted subscriber" that is not equivalent to "unsubscribed subscriber"? - vitidev
  • I add the subscriber so: OnSceneChanged + = func; Delete, in the sense of losing the link to him at home. But this is not causing: OnSceneChanged - = func. OnSceneChanged will not allow the garbage collector to delete the object in this case? - Alerr
  • Yes. will not allow. My first comment is just about that. - vitidev

0