Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3407350
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:44:24+00:00 2026-05-18T05:44:24+00:00

I have written a unit test to comfirm the Dispose on my class does

  • 0

I have written a unit test to comfirm the “Dispose” on my class does unhooks all events and disposes a timer that references the object.

However sometimes WeakReference.IsLive() returns true when I would expect it to return false?

So is there a delay after a full GC before WeakReference.IsLive() is updated?

If not, can you think of anything else that would be giving me unrepeatable results?

WeakReference weekJobWatchDog = new WeakReference(jobWatchDog);
jobWatchDog = null;

// not collected before Dispose called due to timer and events etc
GC.Collect(); GC.Collect();
Assert.IsTrue(weekJobWatchDog.IsAlive);

((IDisposable)weekJobWatchDog.Target).Dispose();

// is now collected as Dispose unlocked all events and dispoed the timer
GC.Collect(); GC.Collect();
Assert.IsFalse(weekJobWatchDog.IsAlive); // sometimes this fails, about 1 in 4 runs

See also Testing Finalizers and IDisposable for a related but different question.

How can I write a unit test to determine whether an object can be garbage collected? has a soltuion that includes calling GC.WaitForPendingFinalizers(), however I rather not call GC.WaitForPendingFinalizers() as I wish to prove that my dispose works and if it worked there will be no need for any finalizers to run.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:44:25+00:00Added an answer on May 18, 2026 at 5:44 am

    This was the issue that I had:

    When you call Dispose() on a System.Timers.Timer it may return before the Win32 timer has been destroyed. Hence there is still a “root” in unmanaged space keeping the timer alive. The time had an event handler that that kept my object alive.

    As this is very timely related, most of the time the Timer would get GCed and so would my object. However sometimes (say 1 in 10 times) the Timer would be kept alive and so would my object.

    A short Sleep() would make my test pass 100% of the time, so does unhooking the event on the timer before disposing it, so the timer can’t keep my object alive.

    see also How do I safely dispose a System.Timers.Timer?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some written a number of unit tests that test a wrapper around
We’ve found that the unit tests we’ve written for our C#/C++ code have really
I have written unit tests under NUnit that use TestFixtures with different parameters. Now,
I am still relatively new to unit testing. I have written a class in
In our application, there are a bunch of unit test console applications that have
I'm just getting into unit testing, and have written some short tests to check
I have written an AIR Application that downloads videos and documents from a server.
I have written a DLL that uses MS Word to spell check the content
I have a couple of unit test helper extension methods, such as, IsNotEmpty(this string
I have written a few MSBuild custom tasks that work well and are use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.