7 Replies - 12812 Views - Last Post: 27 June 2010 - 10:01 AM Rate Topic: -----

#1 Imdsm   User is offline

  • D.I.C Regular
  • member icon

Reputation: 104
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Cannot catch ObjectDisposedException?

Posted 27 June 2010 - 06:22 AM

I'm using Socket.BeginAccept, and then trying to Close the socket later on, but as you may know this causes an exception to be thrown with sockets that are asynchronously waiting for connections. This isn't so much a bug/error as it is a "hello we've been shut down" etc, however, I just cannot catch the exception. :S

Socket workerSocket = listenSocket.EndAccept(result);


This line pulls up an ObjectDisposedException:
Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.

..but I can't seem to handle it!

Does anyone know why the following doesn't catch it?

Thanks!

public void SocketConnected(IAsyncResult result)
{
    try
    {
        Socket listenSocket = (Socket)result.AsyncState;
        Socket workerSocket = listenSocket.EndAccept(result);
        listenSocket.BeginAccept(new AsyncCallback(SocketConnected), listenSocket);

        WriteLog("Connection accepted from " + workerSocket.RemoteEndPoint.ToString());
    }
    catch (Exception e)
    {
        WriteLog("Exception");
    }
}

This post has been edited by Imdsm: 27 June 2010 - 06:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot catch ObjectDisposedException?

#2 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Cannot catch ObjectDisposedException?

Posted 27 June 2010 - 06:25 AM

If it throws an ObjectDisposedException exception, then catch a ObjectDisposedException exception:
try
{
   // etc ..
}
catch (ObjectDisposedException exc)
{
   // do whatever you want with exc
}


Was This Post Helpful? 0
  • +
  • -

#3 Imdsm   User is offline

  • D.I.C Regular
  • member icon

Reputation: 104
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Cannot catch ObjectDisposedException?

Posted 27 June 2010 - 06:38 AM

Already tried that :( still throws the exception, that's what I don't understand..
I can't catch it!

public void SocketConnected(IAsyncResult result)
{
    try
    {
        Socket listenSocket = (Socket)result.AsyncState;
        Socket workerSocket = listenSocket.EndAccept(result);
        listenSocket.BeginAccept(new AsyncCallback(SocketConnected), listenSocket);

        WriteLog("Connection accepted from " + workerSocket.RemoteEndPoint.ToString());
    }
    catch (ObjectDisposedException exc)
    {
        WriteLog("ObjectDisposedException");
    }
    catch (Exception e)
    {
        WriteLog("Exception");
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6258
  • View blog
  • Posts: 24,026
  • Joined: 23-August 08

Re: Cannot catch ObjectDisposedException?

Posted 27 June 2010 - 06:47 AM

Don't know if you've seen this example, but I throw it out there just in case.

The BeginAccept shouldn't be in there...it should be done in the main thread loop. In here is where you start the asynchronous read from the newly connected socket returned from EndAccept.
Was This Post Helpful? 0
  • +
  • -

#5 Imdsm   User is offline

  • D.I.C Regular
  • member icon

Reputation: 104
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Cannot catch ObjectDisposedException?

Posted 27 June 2010 - 07:53 AM

View PostJackOfAllTrades, on 27 June 2010 - 05:47 AM, said:

Don't know if you've seen this example, but I throw it out there just in case.

The BeginAccept shouldn't be in there...it should be done in the main thread loop. In here is where you start the asynchronous read from the newly connected socket returned from EndAccept.


Yep I've seen that, are you saying that BeginAsync is limited to single-threaded applications?

I can't believe that, unless I read it from Microsoft..
Was This Post Helpful? 0
  • +
  • -

#6 Imdsm   User is offline

  • D.I.C Regular
  • member icon

Reputation: 104
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Cannot catch ObjectDisposedException?

Posted 27 June 2010 - 09:12 AM

It would seem that this isn't a problem with the sockets, but no exceptions seem to be catching in this thread. Hmm.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Cannot catch ObjectDisposedException?

Posted 27 June 2010 - 09:39 AM

Maybe because its no longer on this thread. Async is roughly equivalent to saying call me back when you're done.
Was This Post Helpful? 0
  • +
  • -

#8 Imdsm   User is offline

  • D.I.C Regular
  • member icon

Reputation: 104
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Cannot catch ObjectDisposedException?

Posted 27 June 2010 - 10:01 AM

I thought it was a bit iffy, theoretically the code was right. I just stripped down about 8 classes, tested each bit of code, rewrote the entire threading part, with one visual studio getting the error and the other not, only to find out it was a visual studio setting that I'd set at the beginning of the project, that only apply to the project and not every project, totally forgot I'd set it, now I have to piece about 3000 lines back together, AND we just lost the world cup..

Thanks for your help guys, this one is one of them bugs that always gets you, the ones which aren't in the code!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1