[Mono-dev] issue with testing dispose code

Leszek Ciesielski skolima at gmail.com
Wed Mar 4 03:43:37 EST 2009


2009/3/1 Bas Smit <basjsmit at gmail.com>:
> Hi guys,
>
> Im looking at Mono Ninject2 compatibility and Ive come across a problem.
> Nate has a bunch of tests to check his dispose code (example below). For
> this purpose we need a way to force disposing of unused objects. Currently
> the tests use GC.Collect() for that, however on mono that doesnt necessarily
> mean that unused objects are disposed of. Is there a way to force the GC to
> dispose of all unused objects?
>
> Regards, Bas
>
>
> Here is an example of such a (xunit) test:
>
> [Fact]
> public void
> InstancesActivatedWithinScopeAreDeactivatedAfterThreadIsGarbageCollectedAndCacheIsPruned()
> {
>     kernel.Bind<NotifiesWhenDisposed>().ToSelf().InThreadScope();
>     var cache = kernel.Components.Get<ICache>();
>
>     NotifiesWhenDisposed instance = null;
>
>     ThreadStart callback = () => instance =
> kernel.Get<NotifiesWhenDisposed>();
>
>     var thread = new Thread(callback);
>
>     thread.Start();
>     thread.Join();
>
>     thread = null;
>
>     GC.Collect();
>     GC.WaitForPendingFinalizers();
>
>     cache.Prune();
>
>     instance.ShouldNotBeNull();
>     instance.IsDisposed.ShouldBeTrue();
> }

I'm not sure if I understood you correctly: does MS.Net Dispose()
objects that are eligible for garbage collection on calling
"GC.Collect(); GC.WaitForPendingFinalizers();" ? That would be highly
unexpected.
Or is the problem lie in the fact that on Mono
"GC.WaitForPendingFinalizers();" does not really wait for pending
finalizers?


More information about the Mono-devel-list mailing list