-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
I am reading the soure code of DI projects, and i found some code in the class CallSiteRuntimeResolver may leading memory issue, below is the method in this class. If we register a service implemented the IDisposable interface with a transient lifetime, and then get this service from the root ServiceProviderEngineScope, the root scope will hold the reference of this service because the VisitDisposeCache method called Scope.CaptureDisposable, CaptureDisposable's inner logic will add this service to it's _disposables list, so this service will never be collected by GC, if the service has some non-managed resource, this resource will never be disposed.
protected override object VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context)
{
return context.Scope.CaptureDisposable(VisitCallSiteMain(transientCallSite, context));
}I suggest adding an if condition in the VisitDisposeCache method, if the context.Scope is the Root scope then just return the resolved service else call the CaptureDisposable to add the service to scope's _disposables list