Skip to content

Commit

Permalink
Bugfix: When an object that is the scope of another object is release…
Browse files Browse the repository at this point in the history
…d an Exception was thrown.
  • Loading branch information
remogloor committed Mar 25, 2013
1 parent ecdff90 commit 5e0d873
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ReleaseNotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ Version 3.0.2
- Add: ToConstructor() can now accept results from methods as argument e.g. ToConstructor(_ => new Foo(this.GetBar())
- Add: WhenNoAncestorMatches, WhenAnyAncestorMatches and WhenNoAncestorNamed When overloads
- Change: Added WhenAnyAncestorNamed and marked mispelled WhenAnyAnchestorNamed as obsolete
- Bugfix: Private properties of base class were not checked for existence of setter and Inject attribute
- Bugfix: Private properties of base class were not checked for existence of setter and Inject attribute
- Bugfix: When an object that is the scope of another object is released an Exception was thrown.

Version 3.0.1
- Add: The default scope can be changed in the NinjectSettings using
Expand Down
64 changes: 64 additions & 0 deletions src/Ninject.Test/Integration/WhenReleasingAnObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace Ninject.Tests.Integration
{
using System;

using FluentAssertions;

using Xunit;

public class WhenReleasingAnObject
{
private Foo foo;

private StandardKernel kernel;

public WhenReleasingAnObject()
{
this.kernel = new StandardKernel();

this.kernel.Bind<Foo>().ToSelf().InSingletonScope();
this.kernel.Bind<Bar>().ToSelf().InScope(ctx => this.foo);

this.foo = this.kernel.Get<Foo>();
}

[Fact]
public void ItIsDisposed()
{
this.kernel.Release(this.foo);

this.foo.Disposed.Should().BeTrue();
}

[Fact]
public void ObjectsThatHaveItAsScopeAreDisposed()
{
var bar = kernel.Get<Bar>();

this.kernel.Release(this.foo);

bar.Disposed.Should().BeTrue();
}

public class Foo : IDisposable
{
public void Dispose()
{
this.Disposed = true;
}

public bool Disposed { get; set; }
}

public class Bar : IDisposable
{
public void Dispose()
{
this.Disposed = true;
}

public bool Disposed { get; set; }
}

}
}
1 change: 1 addition & 0 deletions src/Ninject.Test/Ninject.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Compile Include="Fakes\ICleric.cs" />
<Compile Include="Fakes\Monk.cs" />
<Compile Include="Fakes\Shield.cs" />
<Compile Include="Integration\WhenReleasingAnObject.cs" />
<Compile Include="Integration\ConstantTests.cs" />
<Compile Include="Fakes\ResolveCountingProvider.cs" />
<Compile Include="Integration\ConstructorArgumentTests.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/Ninject/Activation/Caching/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public bool Release(object instance)
lock(this.entries)
{
var instanceFound = false;
foreach (var bindingEntry in this.entries.Values.SelectMany(bindingEntries => bindingEntries.Values))
foreach (var bindingEntry in this.entries.Values.SelectMany(bindingEntries => bindingEntries.Values).ToList())
{
var instanceEntries = bindingEntry.Where(cacheEntry => ReferenceEquals(instance, cacheEntry.Reference.Instance)).ToList();
foreach (var cacheEntry in instanceEntries)
Expand Down

0 comments on commit 5e0d873

Please sign in to comment.