Results 1 to 6 of 6

Thread: Problem with Disposed objects [Resolved]

  1. #1

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    Resolved 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)"
    Last edited by Porsche944; May 13th, 2005 at 03:17 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    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 ?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    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.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Problem with Disposed objects

    You should do something like...
    VB Code:
    1. 'Behind search form
    2. Private Sub frmSearch_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    3.     Me.Hide()
    4.     otherform.Show
    5. End Sub
    6.  
    7. 'Behind main form
    8. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    9.     ShowFormSelect(sender, e)
    10.     Me.Hide
    11. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width