Disposed forms problem

G

Guest

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 =)
 
A

Andrew S \(Infragistics\)

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 said:
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?
 
G

Guest

Thanks, Andrew. IsDisposed did the trick =)

Andrew S (Infragistics) said:
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.


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'"
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.
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top