0

In my project, when I run it by debugging, after some times, it suddenly has been crashed. It didnt happen in any special time or after specific period time. It crashed itself and I dont know why?!

I got this message:

An unhandled exception of type 'System.ObjectDisposedException' occurred in System.dll
Additional information: Cannot access a disposed object.

and its the picture of that, for more information...

enter image description here

Im working with vs 2013 and C# win form...

Its my simple code :

private void btnConnect_Click(object sender, EventArgs e)
    {
        Result = socketComponent.tcpConnect(Host, int.Parse(Port));
        if (Result == 0)
            MessageBox.Show("Connected"); 
    }

private void btnDisconnect_Click(object sender, EventArgs e)
{
    if (socketComponent != null)
    {
        socketComponent.tcpDisconnect();

        socketComponent.Dispose();
        MessageBox.Show("DisConnected"); 
    }

}

It happened suddenly after click On btnDisconnect. maybe after second click or more ...

MartyIX
  • 27,828
  • 29
  • 136
  • 207
Elahe
  • 1,379
  • 2
  • 18
  • 34
  • seems relevant: http://social.msdn.microsoft.com/Forums/en-US/437c368f-98dc-4b68-9d38-6e81a244c546/systemobjectdisposedexception-crashes-the-application?forum=Vsexpressvcs – James Aug 20 '14 at 07:57
  • Is there no Exception StackTrace available? Should let you track down the issue easily ... – Wolfgang Ziegler Aug 20 '14 at 08:19
  • @WolfgangZiegler I changed the picture. It displays StackTrace. maybe it happend bcz of buffer. is it possible? How can I handle my buffer exceptions... ? – Elahe Aug 20 '14 at 10:02
  • Easy! I think there should have been a null check in the btnConnect_Click event handler as well or perhaps logic to re-create/re-connect the socketComponent. – Alexandru Clonțea Feb 11 '19 at 12:06

2 Answers2

0

My guess is, you should change your code like this:

private void btnDisconnect_Click(object sender, EventArgs e)
{
    if (socketComponent != null)
    {
        socketComponent.tcpDisconnect();

        socketComponent.Dispose();

        // set to null!
        socketComponent = null;
        //

        MessageBox.Show("DisConnected"); 
    }
}

Because otherwise, a second click on "btnDisconnect" will call Dispose a second time which is usually not allowed on an already disposed object. Hence, the exception.

Wolfgang Ziegler
  • 1,675
  • 11
  • 23
  • I tested your code, but didnt fix my problem. It still displays that exception – Elahe Aug 20 '14 at 14:20
  • The exception might still happen if you call connect. You have to check whether socketComponent is null in the connect click as well. Or you recreate the object every time you call connect. – slfan Jun 25 '19 at 06:19
0

The code for the disconnect button includes this:

socketComponent.Dispose();

So the second time (and later) you try to connect the socketComponent it will have been disposed. You are not allowed to use an object after disposing it, so remove that line from the code.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794