Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated UnityMainThreadDispatcher problem #22

Open
ssootube opened this issue Jan 16, 2021 · 0 comments
Open

Duplicated UnityMainThreadDispatcher problem #22

ssootube opened this issue Jan 16, 2021 · 0 comments

Comments

@ssootube
Copy link

ssootube commented Jan 16, 2021

As mentioned in the readme, I use this class by creating an empty game object in a particular scene and attaching UnityMainThreadDispatcher.cs to it.
it doesn't matter if you go over to another scene and never come back to that scene.
but If you go to another scene and then return to that scene, there is a problem : UnityMainThreadDispatcher is duplicated.
Therefore, I modified Awake and onDestroy as below to fix the problem.
I'll leave a source code for those who are experiencing problems like me.

void Awake() {
    if (_instance == null)
    {
        _instance = this;
        DontDestroyOnLoad(this.gameObject);
    }
    else if(this != _instance) //Preserves only the first object created.
        Destroy(this);
}

void OnDestroy() {
    if(this == _instance) // If a duplicate object is destroyed, it does not free the first instance created.
		_instance = null;
}

Below is a scenario in which you can reproduce the problem in the existing code.

Place the empty gameobject with UnityMainThreadDispatcher.cs attached to scene A.

Play the game in scene A.

Awake () from UnityMainThreadDispatcher.cs is called.

Since _instance == null is true, The '_instance' stores the object.

Also, the DontDestroyOnLoad (this.gameObject); is called, so the object does not disappear when we load the scene B.

Now, if you go to scene B and then you come back to scene A,

not only the UnityMainThreadDispatcher object previously maintained by the DontDestroyOnLoad, another UnityMainThreadDispatcher object is created.

Therefore, Awake is executed, but in this case, the _instance is not null so that DontDestroyOnLoad(this.gameObject) is not called.

Moving on to thin B again, the existing UnityMainThreadDispatcher object survives, but the second generation UnityMainThreadDispatcher object that did not run DontDestroyOnLoad is destroyed.

At this time, the second UnityMainThreadDispatcher object was destroyed and OnDestroy was called.

This causes problems when the static variable _instance is initialized to null.

Because if you move back to thin A again, now the '_instance' value is null, so the DontDestroyOnLoad (this.gameObject) is called and two UnityMainThreadDispatchers exist.

If you go back and forth repeatedly like this, you will continue to have a memory leak.

And Even worse, when you are at scene B, then the _instance is null because of OnDestroy() So that you cannot actually call Instance(). it throws error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant