catching AccessViolationException resulting from closing a form

S

Stephan Steiner

Hi

I have written my own RDP Client by simply wrapping the ActiveX in a Windows
Form Library. The lib catches various events thrown by the ActiveX, amongst
other things the OnDisconnected event. Upon that event, I want the form that
hosts the ActiveX to be closed:

private void terminalServiceClient_OnDisconnected(object sender,
AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEvent e)
{
connected = false;
if (loggedIn)
try
{
this.Close();
}
catch (Exception x)
{
Console.WriteLine("exception in closing the current
window");
}
}

This used to work just fine when I was using the Terminal Services Advanced
Client
(http://www.microsoft.com/downloads/...d8-9abc-4e15-a78f-eb2aabad74b5&DisplayLang=de).
However, as part of my migration to .NET 2.0, I recreated the client lib and
had is based on the ActiveX component that is already available in Windows
XP (SP2). Since that upgrade, my users are reporting application crashes in
one particular scenario:

They open a remote desktop to some machine. If they then just close the
remote desktop form, or if they have it closed via the launching application
(the application has a button that amongst other things disconnects the
remote desktop component, then closes the hosting form), all works as
expected. If however, they log out from the remote machine, the application
crashes. In debugging, I've found that the above code runs through without
problem, then the next thing that happens is that I get an uncaught
AccessViolationException on the Application level of the launching
application (so Application.Run()). Even though I have configured my
application to catch any uncaught errors, this exception isn't caught.
Here's the launching block of my application.

static void Main()
{
Application.EnableVisualStyles();
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.Run(new Form1());
}

The full details of the exception are as follows:

Attempted to read or write protected memory. This is often an indication
that other memory is corrupt.


at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at RemoteSupportClient.Form1.Main() in
D:\Temp2\Remotesupport\RemoteSupportClient\Form1.cs:line 476
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

If I comment out

this.Close

in terminalServiceClient_OnDisconnected, things work out fine, but then the
form isn't closed when the remote desktop is disconnected.

I'm wondering, how I could catch this AccessViolationException on this
level? Even with a try/catch around the Application.Run, I can't solve this
as then the app would exit upon receiving that exception (which is nicer
than crash but still not what I want), and why isn't it already catched by
the Application_ThreadException event handler?

Regards
Stephan
 

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