Skip to content

Possible memory leak in ZenPools #83

@ZumiKua

Description

@ZumiKua

Describe the bug
When despawn LookupId to ZenPools, LookupId's Provider field may contain a reference to the injected instance, which will leak that reference.

To Reproduce
Just inject any instance with AsSingle scope, you will find that that instance will not be collected even it is out of scope.
For example, with the following Installer attached to a Scene Context, goto that scene, then switch to another scene, you will find that ShouldNotLeak still be referenced.

public class Installer : MonoInstaller
{
    public override void InstallBindings()
    {
        Container.Bind<ShouldNotLeak>().AsSingle().NonLazy();
    }
}

Expected behavior
Inject instance should be collected when out of scope.

Screenshots
leak
(Heap captured by Heap Explorer)

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

  • Zenject version: 9.1.0
  • Unity version: 2018.4.36f1
  • Project's scripting backend [e.g. Mono/IL2CPP] Mono

Additional context
To fix this leak, simply add these two lines in the ZenPools.cs.

Index: Assets/Plugins/Zenject/Source/Util/ZenPools.cs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Assets/Plugins/Zenject/Source/Util/ZenPools.cs b/Assets/Plugins/Zenject/Source/Util/ZenPools.cs
--- a/Assets/Plugins/Zenject/Source/Util/ZenPools.cs	(revision 072a4de9796b65eaa85dfc44ba8f91dc4d6e09a6)
+++ b/Assets/Plugins/Zenject/Source/Util/ZenPools.cs	(date 1704437947544)
@@ -137,6 +137,8 @@
 
         public static void DespawnLookupId(LookupId lookupId)
         {
+            lookupId.BindingId = default;
+            lookupId.Provider = null;
             _lookupIdPool.Despawn(lookupId);
         }
 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions