Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

'System.ObjectDisposedException' : Cannot access a disposed object

1 view
Skip to first unread message

Oliver Ballarin via .NET 247

unread,
Dec 22, 2004, 6:57:06 PM12/22/04
to
Hi gurus!!!

I have a winform Application that starts with a Connection Form and then instanciates the Main Form.
When closing the main form by clicking the famous top right cross, it works fine but when i use the disconnect button (to go back to the connection form),
i have the exception System.ObjectDisposedException : Cannot access a disposed object.

I have read that it has a relation with the GC and/or the Dispose method but i'm a beginner in .net and i don't really understand the real problem.

If somebody has an idea to help me, it would be great!!!!

Thx a lot

Here is a piece of code (hope can help my explanation 8-) )

public class FConnect: System.Windows.Forms.Form
{
static void Main()
{
RemotingConfiguration.Configure(filename);
FConnect connect = new FConnect();
Application.Run(connect);
}
private bConnect_Click(object sender, System.EventArgs e)
{
FMain main = new FMain();
main.ConnectOK();
//Set the handle ConnectForm to the Main
main.ConnectForm = this;
//Hide the connect form
this.Hide();
//Show main form
main.Show();
}
}
//Main Form
public class FMain : System.Windows.Forms.Form
{
public FConnect ConnectForm;
private bDisConnect_Click(object sender, System.EventArgs e)
{
this.ConnectForm.Show();
this.Close();
}
private void FMain_Closed(object sender, System.EventArgs e)
{
this.ConnectForm.Show();
}

}

--------------------------------
From: Oliver Ballarin
Ballarin

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>d5LmE8R4a0K2p9D9YBUeuw==</Id>

Dmitriy Lapshin [C# / .NET MVP]

unread,
Dec 23, 2004, 3:38:48 AM12/23/04
to
Hi,

This means that you are trying to use an object that has been disposed. Not
every object is disposable, so it is a sort of clue what object can it be.
From your code I can suppose that you keep referencing one form from
another, and you try to access the other form from the first one when the
other form has been already closed by the user (or programmatically).

The form gets disposed when it is closed, because every form consumes
unmanaged resources (at least one User32 object - namely a window), which
should be freed as soon as the form is no longer needed. Therefore, after
the form is closed, it becomes unusable and it would be better to null out
all references to the form (or at least assume they are no longer valid).

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Oliver Ballarin via .NET 247" <anon...@dotnet247.com> wrote in message
news:OcZHyHI6...@TK2MSFTNGP10.phx.gbl...

0 new messages