Problem with Disposed objects [Resolved]
Hey everyone,
I created a program that searches Active Directory. I built the release version and it runs as it should. The problem comes up if I attempt to reopen the search form from the system tray again I get this error.
"Cannot access a disposed object named "Search"
I think the problem is coming up when the garbage collection runs it deletes the Search Form object out of memory. How can I prevent this from happening in my program?
"just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.ObjectDisposedException: Cannot access a disposed object named "Search".
Object name: "Search".
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Form.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at Active_Directory_Advanced_Search.sysTray.ShowFormSelect(Object sender, EventArgs e)
at Active_Directory_Advanced_Search.sysTray.NotifyIcon_DoubleClick(Object sender, EventArgs e)
at System.Windows.Forms.NotifyIcon.OnDoubleClick(EventArgs e)
at System.Windows.Forms.NotifyIcon.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.NotifyIcon.WndProc(Message& msg)
at System.Windows.Forms.NotifyIconNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)"
Re: Problem with Disposed objects
Either create a new instance each time you want to show it (since you are disposing it upon close of that form, probably) or create
a public var set to the search form and create it on first use. Then just do a .Show/.Hide instead of disposing of it.
Re: Problem with Disposed objects
How should I change my code to just show and hide the form ?
Here is my code as it is now.
In System Tray Form
Code:
Private Sub ShowFormSelect(ByVal sender As Object, ByVal e As System.EventArgs)
'Display SettingsForm dialog if it is not already displayed.
If mSettingsForm Is Nothing Then
mSettingsForm = New Search
mSettingsForm.ShowDialog()
Else
If mSettingsForm.Visible = False Then
mSettingsForm.Visible = True
End If
mSettingsForm.Activate()
End If
End Sub
In system tray form Declarations
Code:
Private mSettingsForm As Search
Under what event in the search form should I put
me.hide ?
Re: Problem with Disposed objects
In the close button's click event, but its being shown modally so that will hold up the process.
Will it messup any of your other code to just do a .Show instead of a .ShowDialog?
You could hide the calling form from the form that is calling the search and show it again upon the close buttons click
event in the search form.
Re: Problem with Disposed objects
No I went ahead and changed it from showdialog to show. I can't find the event for the on click close command. All I see in the event viewer is closed and closing i tried me.hide in each but i still get this error.
Re: Problem with Disposed objects
You should do something like...
VB Code:
'Behind search form
Private Sub frmSearch_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Me.Hide()
otherform.Show
End Sub
'Behind main form
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
ShowFormSelect(sender, e)
Me.Hide
End Sub