modesttree / Zenject

Dependency Injection Framework for Unity3D
MIT License
2.55k stars 273 forks source link

Editor crash in ZenUtilInternal.GetRootGameObjects #176

Open cathei opened 4 years ago

cathei commented 4 years ago

Describe the bug Unity Editor crashes when ZenUtilInternal.GetRootGameObjects called on some occasions.

To Reproduce Sorry but it's hard to describe the reproducing step. This sometimes happens and sometimes doesn't, usually happens after you re-open the project. I'm writing more details on Additional context below.

Expected behavior No crash.

Extenject and Unity info (please complete the following information):

Additional context The crash happens when SceneContext tries to inject on root game objects. Specifically this line is making the crash for me.

return Resources.FindObjectsOfTypeAll<GameObject>()
    .Where(x => x.transform.parent == null
        && x.GetComponent<ProjectContext>() == null
        && x.scene == scene);

Obviously Resources.FindObjectsOfTypeAll returns bunch of internal objects, persistent objects. After some experiments, I assumed some of these objects are 'not preprared' to call GetComponent and found the below works for me.

return Resources.FindObjectsOfTypeAll<GameObject>()
    .Where(x => x.scene == scene
        && x.transform.parent == null
        && x.GetComponent<ProjectContext>() == null);

Which filters by scene first (As the code assumes persistent/internal objects doesn't have scene property) and then access to transform and components.

I couldn't confirm it all while Unity is keep crashing, but I'm willing to add more information or to make pull request later.

manuelgoellnitz commented 4 years ago

I have the same issue on Unity 2020.1.x (have it with all Versions so far). I have two scenes, a main and secondary. When I start playmode with the main scene open and load the secondary one during runtime everything works fine. But when I start playmode with secondary scene open, the editor crashes on this line with SIGSEGV.

It looks like this construction is called when the scene is not ready yet

The workraound seems to work for our project as well! Many Thanks cathei

AmitSuri commented 2 years ago

Testing on Unity 2022.1.5f1 .. this still does not work on iOS build even after applying the work around. Is this project dead?