I am struggling to understand where my memory leak is coming from. I have a modified version of a post I came across while researching. We use StructureMap, we want to use AOP for MiniProfiler so this seemed like a perfect thing to try but when I implemented it, I started get massive memory leaks. I tracked down the leak to the point where I am creating the proxy. If I leave all of the other code the same and just remove the .EnrichWith(...)
call, the memory leak goes away. I am creating the proxy class in a Bootstrapper class like so:
x.For<IServiceInterface>()
.Use<ServiceClass>()
.EnrichWith(ex => DynamicProxyHelper.
CreateInterfaceProxyWithTargetInterface(typeof(IServiceInterface), ex));
The Dynamic Proxy Helper Class:
public class DynamicProxyHelper
{
public static IEnumerable<Type> GetScannableTypes()
{
var types = Assembly.GetExecutingAssembly().GetTypes();
var scannableTypes = new List<Type>();
foreach (var type in types)
{
// http://www.hanselman.com/blog/DoesATypeImplementAnInterface.aspx
if (typeof (IAttributeScanTask).IsAssignableFrom(type)
&& type.FullName != typeof (IAttributeScanTask).FullName)
{
scannableTypes.Add(type);
}
}
return scannableTypes;
}
public static object CreateInterfaceProxyWithTargetInterface<T>(Type interfaceType, T obj)
{
if (!interfaceType.IsInstanceOfType(obj))
{
throw new ArgumentException(
"DynamicProxyHelper: Object passed to the proxy must inherit from the interface type passed to the proxy.");
}
// Create the proxy and return the result
var dynamicProxy = new ProxyGenerator();
var scannableTypes = GetScannableTypes();
var result = dynamicProxy.CreateInterfaceProxyWithTargetInterface(
interfaceType,
obj,
new IInterceptor[] { new MyInterceptor(obj.GetType(), new AttributeScanEngine(), scannableTypes)} );
return result;
}
}
And the MyInterceptor Class:
public interface IMyInterceptor : IInterceptor {}
public class MyInterceptor : IMyInterceptor
{
private readonly Type _concreteType;
private readonly IAttributeScanEngine _scanEngine;
private readonly IEnumerable<Type> _scannableTypes;
private const string AttributeNameSpace = "MyAttributes";
public MyInterceptor() : this(typeof(object), new AttributeScanEngine(), new List<Type>()){}
public MyInterceptor(Type concreteType, IAttributeScanEngine scanEngine, IEnumerable<Type> scannableTypes)
{
_concreteType = concreteType;
_scanEngine = scanEngine;
_scannableTypes = scannableTypes;
}
public void Intercept(IInvocation invocation)
{
var scanType = ResolveScanType(invocation);
// We found a matching attribute that can be applied
if (scanType != null)
{
// execute the custom task we need to run
_scanEngine.Run(invocation, scanType, _concreteType);
}
else
{
// no scanned types could be found so execute the method as is
invocation.Proceed();
}
}
protected internal virtual Type ResolveScanType(IInvocation invocation)
{
foreach (var type in _scannableTypes)
{
var attributeName = GetAttributeName(type.Name);
var attributeType = Type.GetType(attributeName);
if (attributeType != null)
{
var attributeDecoration = Attribute.GetCustomAttribute(invocation.GetConcreteMethodInvocationTarget(), attributeType, true);
// We found an attribute for this scan type
if (attributeDecoration != null)
{
return type;
}
}
}
return null;
}
protected internal virtual string GetAttributeName(string typeName)
{
var aspectName = typeName.Substring(0, typeName.IndexOf("ScanTask"));
return AttributeNameSpace + "." + aspectName + "Attribute";
}
}
I believe it's related to the creation of the proxy via the call to EnrichWith
because if I leave all other parts of the code the same and simply remove that call, the memory leak goes away. Is there something fundamental I'm doing wrong here?