tags:

views:

227

answers:

2

I'm trying to figure out how when simply showing a WinForms dialog (code below) I get the following Exception and callstack. This doesn't happen all the time, but I'm seeing it in my exception logs. Any ideas? I can't figure out what would be referencing a disposed object?

I've verified (via the rest of the callstack) that the application is not shutting down, it is running normally.

System.ObjectDisposedException: Cannot access a disposed object.    
Object name: 'MainForm'.    
   at System.Windows.Forms.Control.CreateHandle()    
   at System.Windows.Forms.Form.CreateHandle()    
   at System.Windows.Forms.Control.get_Handle()    
   at System.Windows.Forms.Control.GetSafeHandle(IWin32Window window)    
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)    
   at MyApp.MainForm.PromptForProfile()    
   at MyApp.MainForm.LoadProfile()    
   at MyApp.MainForm.barButtonItem1_ItemClick(Object sender, ItemClickEventArgs e)

This is the code for the dialog being displayed. The only "goofy" code is probably the textPassword_KeyDown handler. I should probably pull the code I want out and not call btnOK_Click that way.

public partial class ProfileForm : DevExpress.XtraEditors.XtraForm
   {
      public string _username;
      public string _password;

      public ProfileForm()
      {
         InitializeComponent();
      }

      private void btnOK_Click( object sender, EventArgs e )
      {
         _username = textUsername.Text;
         _password = textPassword.Text;
      }

      private void textPassword_KeyDown( object sender, KeyEventArgs e )
      {
         if ( e.KeyCode == Keys.Enter )
         {
            btnOK_Click( sender, null );
            this.DialogResult = DialogResult.OK;
            e.Handled = true;
         }
      }

      private void hyperLinkEdit1_Click( object sender, EventArgs e )
      {
         // show the proxy settings dialog
         ProxyForm pform = new ProxyForm();         
         pform.ShowDialog();         
      }
   }
A: 

Well, one possibilily is that you're setting DialogResult to Ok, which will close the form, but you then refer to the eventarg triggered by pressing Enter. I'm not too sure of the role of the hyperlink edit1 bit, though. Is it on the same form, or a calling form?

CodeByMoonlight
A: 

Your stack trace tells me that you aren't getting into the ProfileForm code. It's failing on some control's CreateHandle. Without more information, I can only guess:

  1. Verify that you're performing all your UI manipulation is occurring on your GUI thread. Even if you think it is, double check. (Sometimes the threading can be subtle.)

  2. Make sure that you aren't trying to display the same form instance twice, the second time after it's already been disposed. I see that you've got a ShowDialog() happening, but if you're trying to ShowDialog() on a form that's already been disposed, I'd expect it to explode like this.

  3. Ensure that any usercontrols on the form behave properly.

  4. Consider using a secure string for your password field.

Greg D