Object disposed exception

  • Thread starter Thread starter Simanovsky
  • Start date Start date
S

Simanovsky

Hello,

I have a form with a control on it. I close the form in
the deactivate event handler. I also close the form in
control double-click event. In the form deactivate event
handler I ensure that the form is not closed twice using
my own flag.
In debug the form code seems to have the correct flow
(Dispose() method is called only once), but after my code
exits an exception is raised telling that my disposed
control is accessed.
What may be the possible reasons for that?

Thanks in advance,
Andrey.
 
-----Original Message-----
Hello,

I have a form with a control on it. I close the form in
the deactivate event handler. I also close the form in
control double-click event. In the form deactivate event
handler I ensure that the form is not closed twice using
my own flag.
In debug the form code seems to have the correct flow
(Dispose() method is called only once), but after my code
exits an exception is raised telling that my disposed
control is accessed.
What may be the possible reasons for that?

Thanks in advance,
Andrey.
.
 
Simanovsky said:
In debug the form code seems to have the correct flow
(Dispose() method is called only once), but after my code
exits an exception is raised telling that my disposed
control is accessed.
What may be the possible reasons for that?


Dispose patterns should be robust to multiple calls, therefore calling
Dispose over and over should not be a problem. What is the stack trace for
the exception? You should be able to use the call stack to pinpoint the
exact line of code that is attempting to access the disposed object.
 
Is this your application's main form (the one you pass to
Application.Run())?

If so, be aware that Application.Run() automatically calls Dispose on
the form after the form closes. If you only want your form to get
disposed once, you should Close it in your Deactivate handler, instead
of Disposing it.

I think ShowDialog() also contains an automatic Dispose() call.
 
Back
Top