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

  • SEARCH
  • Home
  • 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 97325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:59:00+00:00 2026-05-10T23:59:00+00:00

I’m working with some code (not mine I hasten to add, I don’t trust

  • 0

I’m working with some code (not mine I hasten to add, I don’t trust this much at all) for a class which opens a socket, makes requests and listens for responses, which is throwing an exception in a way I can’t comprehend when tested in xunit. I assume the same exception happens ‘live’ but the class is referenced by a singleton so it is probably just hidden.

The problem manifests as ‘System.CannotUnloadAppDomainException: Error while unloading appdomain’ in xunit and the inner exception is ‘System.ObjectDisposedException’ thrown (essentially) inside the finaliser when closing the socket! There are no other reference to the socket which call close and dispose is protected on the Socket class so I’m not clear how else the object could be disposed.

Further, if I merely catch and absorb the ObjectDisposedException xunit terminates when it hits the line to close the listener thread.

I just don’t get how the Socket can be disposed before it’s asked to close.

My knowledge of sockets is only what I’ve learnt since finding this problem, so I don’t know if I’ve provided everything SO might need. LMK if not!

public class Foo {     private Socket sock = null;     private Thread tListenerThread = null     private bool bInitialised;     private Object InitLock = null;     private Object DeInitLock = null;      public Foo()     {         bInitialised = false;          InitLock = new Object();         DeInitLock = new Object();     }      public bool initialise()     {         if (null == InitLock)             return false;          lock (InitLock)         {             if (bInitialised)                 return false;              sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);             sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 8);             sock.Bind( /*localIpEndPoint*/);             sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(mcIP));              tListenerThread = new Thread(new ThreadStart(listener));             tListenerThread.Start();              bInitialised = true;             return true;         }     }      ~Foo()     {         if (bInitialised)             deInitialise();     }      private void deInitialise()     {         if (null == DeInitLock)             return;          lock (DeInitLock)         {             if (bInitialised)             {                 sock.Shutdown(SocketShutdown.Both); //throws System.ObjectDisposedException                 sock.Close();                  tListenerThread.Abort(); //terminates xunit test!                 tListenerThread = null;                  sock = null;                  bInitialised = false;             }         }     } } 
  • 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. 2026-05-10T23:59:01+00:00Added an answer on May 10, 2026 at 11:59 pm

    If this object is eligible for garbage collection and there are no other references to the Socket, then the socket’s finalizer may well run before your object’s finalizer. I suspect that’s what’s happened here.

    It’s generally a bad idea (IMO) to do this much work in a finalizer. I can’t remember the last time I implemented a finalizer at all – if you implement IDisposable, you should be fine unless you have direct references to unmanaged resources, which are almost always in the form of IntPtrs. Orderly shutdown should be the norm – a finalizer should only usually run if either the program is shutting down, or someone has forgotten to dispose of the instance to start with.

    (I know you clarified at the start that this isn’t your code – I just thought I’d explain why it’s problematic. Apologies if you already knew some/all of this.)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but

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.