ControlController MVC3 IControllerFactory memory leak error
In our project we are using MefControllerFactory, these are the codes we set in application_start in global.asax as ControllerBuilder.Current.SetControllerFactory(new Project.Helper.MefControllerFactory(container));
The problem is that running the .NET Profiler results in a memory leak issue even though we dispose of the controllers in the "ReleaseController" method. Original message: "The instance is located, but is still reachable from one or more roots. Since the selected instance is usually no longer in use, this could indicate a memory leak. Examine the instance to see if it is inadvertently persisting, or if the problem may be ignored. " Is there a solution to this problem?
Second post: "The mentioned instance with direct roots of EventHandler The mentioned instance is directly injected by the EventHandler, that is, the instance is only used as an EventHandler object and cannot be reached from any other root without going through a delegate. cause memory leaks, this problem is a strong symptom of a memory leak. "
public class MefControllerFactory : IControllerFactory
{
private CompositionContainer _container;
private DefaultControllerFactory defaultControllerFactory;
public MefControllerFactory(CompositionContainer container)
{
_container = container;
this.defaultControllerFactory = new DefaultControllerFactory();
}
#region IControllerFactory Members
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
IController controller = null;
controller = _container.GetExportedValueOrDefault<IController>(controllerName);
if (controller == null)
return this.defaultControllerFactory.CreateController(requestContext, controllerName);
return controller;
}
public void ReleaseController(IController controller)
{
IDisposable disposable = controller as IDisposable;
if (disposable != null)
disposable.Dispose();
}
#endregion
public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
{
return System.Web.SessionState.SessionStateBehavior.Default;
}
}
Thank...
source to share
Here's a MEF Factory based controller I created for code samples for my book :
public class MefControllerFactory : DefaultControllerFactory
{
private readonly CompositionContainer container;
private readonly Dictionary<IController, Lazy<object, object>> exports;
private readonly object syncRoot;
public MefControllerFactory(CompositionContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
this.exports = new Dictionary<IController, Lazy<object, object>>();
this.syncRoot = new object();
}
protected override IController GetControllerInstance(
RequestContext requestContext, Type controllerType)
{
var export = this.container.GetExports(
controllerType, null, null).Single();
var controller = (IController)export.Value;
lock (this.syncRoot)
{
this.exports.Add(controller, export);
}
return controller;
}
public override void ReleaseController(IController controller)
{
lock (this.syncRoot)
{
var export = this.exports[controller];
this.exports.Remove(controller);
this.container.ReleaseExport(export);
}
base.ReleaseController(controller);
}
}
In MEF, you can only drop object graphs by releasing Export, not the exported value.
source to share