Object disposed exception

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.
 
P

pouya

-----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.
.
 
K

Kevin Westhead

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.
 
J

Joe White

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.
 

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