Skip to content

Commit

Permalink
Prevent garbage collector to collect DynamicMethod produced by advisor
Browse files Browse the repository at this point in the history
  • Loading branch information
Puresharper committed Mar 30, 2017
1 parent 29dad2c commit 9f08b6e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions NConcern/NConcern.Example/NConcern.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -46,7 +48,17 @@
<Compile Include="Calculator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\CNeptune.1.0.5\build\CNeptune.targets" Condition="Exists('..\packages\CNeptune.1.0.5\build\CNeptune.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\CNeptune.1.0.5\build\CNeptune.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNeptune.1.0.5\build\CNeptune.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
4 changes: 4 additions & 0 deletions NConcern/NConcern.Example/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CNeptune" version="1.0.5" targetFramework="net452" />
</packages>
4 changes: 4 additions & 0 deletions NConcern/NConcern/Aspect/Directory/Aspect.Directory.Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static private FieldInfo Pointer(MethodBase method)
public readonly MethodBase Method;
public readonly Activity Activity;
private readonly LinkedList<IAspect> m_Aspectization;
private readonly LinkedList<MethodInfo> m_Sequence;
private readonly Dictionary<IAspect, Activity> m_Dictionary;
private readonly IntPtr m_Pointer;
private readonly FieldInfo m_Field;
Expand All @@ -58,16 +59,19 @@ unsafe internal Entry(Type type, MethodBase method, Activity activity)
if (_type == null) { throw new NotSupportedException(string.Format("type '{0}' is not managed by CNeptune and cannot be supervised.", type.AssemblyQualifiedName)); }
this.m_Field = Pointer(method);
this.m_Pointer = (IntPtr)this.m_Field.GetValue(null);
this.m_Sequence = new LinkedList<MethodInfo>();
}

private void Update()
{
var _aspectization = this.m_Aspectization.SelectMany(_Aspect => _Aspect.Advise(this.Method)).ToArray();
var _pointer = this.m_Pointer;
this.m_Sequence.Clear();
foreach (var _advice in _aspectization)
{
if (_advice == null) { continue; }
var _method = _advice.Decorate(this.Method, _pointer);
this.m_Sequence.AddLast(_method);
if (_method != null) { _pointer = _method.Pointer(); }
}
this.m_Field.SetValue(null, _pointer);
Expand Down

0 comments on commit 9f08b6e

Please sign in to comment.