Re: main form, IDisposable, and Application.Run



Peter Duniho wrote:
On Mon, 11 Feb 2008 13:54:33 -0800, Ben Voigt [C++ MVP]
<rbv@xxxxxxxxxxxxx> wrote:

[...]
So it's not like the form isn't going to be disposed - unless an
exception is thrown between construction and the method call.

I'm beginning to suspect that the Form is finalized, not disposed,
if an exception is thrown anywhere in the message loop.

I haven't looked at the Application.Run() method via Reflector (or any
other for that matter :) ), but given the documentation, it seems
reasonable to believe that it deals with disposing of form instances
even in the exceptional case.

I know that exceptions are dealt with differently in the GUI thread
than in other threads. They must be because there's the
ThreadException event that you can subscribe to, and if there's a
handler for that event, GUI thread exceptions that occur during the
dispatch of window messages are simply reported and don't cause the
application to exit.
It seems plausible (you could check this in Reflector, I guess :) )
that when the event is _not_ handle, the way the application exits is
either through rethrowing the exception (in which case a "using" or
try/catch/finally block in the Run() method or something it calls
would close and/or dispose the form), or by explicitly closing all
open forms (which would obviously dispose the form).

And even if not, since the project wizard outputs code that will
simply exit the application once the Run() method returns, it's
difficult to see why the form should be required to be disposed at
that point anyway. Personally, I am generally in the habit of
cleaning resources up before exiting. However, there's a good
argument in favor of just not bothering...the OS will clean them up
when the process exits anyway, and it just delays the exit of your
process to clean stuff up that is in use.
Tracing through the
relevant framework code in Reflector isn't going very well, there's a
lot of
it.

Maybe the exception is being thrown from the constructor after all,
in which
case a containing using block won't help.

Isn't it a requirement that if the constructor throws an exception,
any unmanaged resources in the class are to be disposed of by the
class itself?

Yeah, I think so. I'll have to review the constructor and make sure that
Dispose gets called appropriately on member objects if an exception is
thrown. I wish this class was C++/CLI...


I'm curious: is there some specific scenario you're running into that
prompts the question? Are you seeing a form not get disposed when
it's a problem for it not to be? If so, how is that happening?

I'm getting exception dumps where the call stack begins from
Component.Finalize, which isn't very helpful for understanding the overall
state of the app.


Pete


.



Relevant Pages

  • Re: main form, IDisposable, and Application.Run
    ... using (MyFormClass mainForm = new MyFormClass) ... The Dispose method of the Form class will be called prior to the ... exception is thrown between construction and the method call. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Create/Exception then Destruct
    ... > When an exception is raised during creation of an object, Destroy is ... > automatically called to dispose of the unfinished object. ... Because a constructor sets the fields of a new object to zero ... > destructor after an exception in the constructor. ...
    (comp.lang.pascal.delphi.misc)
  • Re: if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.Stre
    ... If my System.IO.StreamWriter Write method throws "The specified network name ... clause the close or dispose method just throws "The specified network name ... >> if System.IO.StreamWriter write throws an exception, ... no matter if the exception was thrown ...
    (microsoft.public.dotnet.xml)
  • RE: Wrapping Word.Application object with an IDisposable class?
    ... there is an unhandled exception in my program, ... Word process is not closed in Dispose method when the application is tested ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.vb)
  • Re: is such exception handling approach good?
    ... I agree with you that there is no exception thrown from constructor. ... there may be exception thrown from other initialization methods, ...
    (microsoft.public.vc.language)

Loading