I have a simple problem:

In calling form:

Private _theForm As MyFormClass()
' in a button click event...
If theForm Is Nothing Then
theForm = New MyFormClass()
End If
theForm.Show()

The problem is if I close theFrom, then click the button of the calling form again, theForm Is Nothing will evaluate to False (meaning it's a valid reference), yet when theForm.Show() executes, I get an exception "Cannot access disposed object named 'theForm'"

Even if I call Dispose on the called form when it's closed, the problem doesn't go away. I thought it was just some garbage collection issue but calling GC.Collect/WaitForPendingFinalizers before theForm.Show() does not make any difference.

As a workaround, I could trap the exception in a try block but I was hoping for a more elegant solution, like simply inspecting a property. In the first place, why does theForm Is Nothing evalute to False when the form is already closed/disposed?

Many thanks =)

Re: Disposed forms problem by Andrew

Andrew
Tue Jun 22 08:46:09 CDT 2004

When an object is disposed, the object still exists and will not be
completely cleaned up until all references (rooted references anyway) to the
object are removed. In this case, you can checked the IsDisposed property of
the form to see if it has been disposed.
e.g.
If theForm Is Nothing OrElse theForm.IsDisposed Then

An alternative approach would be to catch the Disposed event of the form
from the class that is holding a reference to the form and when the event is
invoked, unhook from the disposed event and clear the member variable that
references that form so that it is nothing/null.

"jester" <jester@discussions.microsoft.com> wrote in message
news:CF2E450B-857B-4C4C-B365-DC6B92B43C46@microsoft.com...
>
> I have a simple problem:
>
> In calling form:
>
> Private _theForm As MyFormClass()
> ' in a button click event...
> If theForm Is Nothing Then
> theForm = New MyFormClass()
> End If
> theForm.Show()
>
> The problem is if I close theFrom, then click the button of the calling
form again, theForm Is Nothing will evaluate to False (meaning it's a valid
reference), yet when theForm.Show() executes, I get an exception "Cannot
access disposed object named 'theForm'"
>
> Even if I call Dispose on the called form when it's closed, the problem
doesn't go away. I thought it was just some garbage collection issue but
calling GC.Collect/WaitForPendingFinalizers before theForm.Show() does not
make any difference.
>
> As a workaround, I could trap the exception in a try block but I was
hoping for a more elegant solution, like simply inspecting a property. In
the first place, why does theForm Is Nothing evalute to False when the form
is already closed/disposed?
>
> Many thanks =)



Re: Disposed forms problem by jester

jester
Wed Jun 23 01:41:02 CDT 2004


Thanks, Andrew. IsDisposed did the trick =)

"Andrew S (Infragistics)" wrote:

> When an object is disposed, the object still exists and will not be
> completely cleaned up until all references (rooted references anyway) to the
> object are removed. In this case, you can checked the IsDisposed property of
> the form to see if it has been disposed.
> e.g.
> If theForm Is Nothing OrElse theForm.IsDisposed Then
>
> An alternative approach would be to catch the Disposed event of the form
> from the class that is holding a reference to the form and when the event is
> invoked, unhook from the disposed event and clear the member variable that
> references that form so that it is nothing/null.
>
> "jester" <jester@discussions.microsoft.com> wrote in message
> news:CF2E450B-857B-4C4C-B365-DC6B92B43C46@microsoft.com...
> >
> > I have a simple problem:
> >
> > In calling form:
> >
> > Private _theForm As MyFormClass()
> > ' in a button click event...
> > If theForm Is Nothing Then
> > theForm = New MyFormClass()
> > End If
> > theForm.Show()
> >
> > The problem is if I close theFrom, then click the button of the calling
> form again, theForm Is Nothing will evaluate to False (meaning it's a valid
> reference), yet when theForm.Show() executes, I get an exception "Cannot
> access disposed object named 'theForm'"
> >
> > Even if I call Dispose on the called form when it's closed, the problem
> doesn't go away. I thought it was just some garbage collection issue but
> calling GC.Collect/WaitForPendingFinalizers before theForm.Show() does not
> make any difference.
> >
> > As a workaround, I could trap the exception in a try block but I was
> hoping for a more elegant solution, like simply inspecting a property. In
> the first place, why does theForm Is Nothing evalute to False when the form
> is already closed/disposed?
> >
> > Many thanks =)
>
>
>