Dave, thanks for your response.
Bellow is sample code. If you pass any argument on comman line, it works ok.
If you run it with no arg, IndexOutOfRangeEception is thrown, and, on my
machine (.NET 1.1), I cannot see that ~ReleaseMe finalizer and
CurrentDomain_ProcessExit handler were invoked.
(Also, if I do not setup console event handler, CTRL-C also kills the app
without finalizer and handler get called.)
What am I doing wrong? Now, in order to make my app working somehow, I have
console event, process exit event and unhandled exception event handlers to
clean those sensitive native resources on vm exit...
Thanks,
Ales
---------------------------
namespace CleaningUp
{
class ReleaseMe
{
~ReleaseMe()
{
Console.WriteLine("~ReleaseMe");
}
}
class Class1
{
static void CurrentDomain_ProcessExit(object sender, EventArgs args)
{
Console.WriteLine("CurrentDomain_ProcessExit");
}
[STAThread]
static void Main(string[] args)
{
ReleaseMe rm = new ReleaseMe();
AppDomain.CurrentDomain.ProcessExit += new
EventHandler(CurrentDomain_ProcessExit);
args[0] += "throw exception, now!";
Console.WriteLine("press any key to exit");
Console.Read();
}
}
}
> The best way to ensure that native resources get cleaned up is to wrap each
> native resource in a managed class, provide a Finalizer method for each one,
[quoted text clipped - 22 lines]
> > Many thanks,
> > Ales Pour
Dave - 16 Dec 2003 20:12 GMT
I get much the same results. The reason is that when you throw an unhandled
exception the main thread it causes an unclean termination. If you attach an
unhandled exception handler you will see it.
System.AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
public static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
Console.WriteLine("UnhandledException. AppDomain: {1}, IsTerminating?: {0}",
e.IsTerminating,AppDomain.CurrentDomain.FriendlyName);
Exception ex = (Exception)e.ExceptionObject;
Console.WriteLine("{0}",ex.Message);
}
An abnormal termination means that you don't follow the normal shutdown
logic; things just stop. You can wrap your main with a try-catch handler so
that you avoid this.
> Dave, thanks for your response.
>
[quoted text clipped - 78 lines]
> > > Many thanks,
> > > Ales Pour