Results 1 to 14 of 14

Thread: [RESOLVED] Object Disposed Exception - Multiple Forms

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2010
    Posts
    26

    Resolved [RESOLVED] Object Disposed Exception - Multiple Forms

    Hey Guys,

    I am using VB 2008 Express Edition.

    I have an app that has 2 forms (will be adding more). Button click on form1 opens form 2. The first time I run my app everything works fine, but when I click the close 'X' button on form 2 and try opening form 2 again by clicking the button on form 1, VB throws a Object Disposed Exception, Cannot access a disposed object

    Here is my code on form 1

    Code:
    Public Class frmMain
        Dim myForm As Form3
        Private Sub btnWeld1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeld1.Click
            If IsDisposed Then Return
            If myForm Is Nothing Then
                myForm = New Form3
            End If
    
            myForm.Show()
        End Sub
    
    End Class
    Can you tell me how to fix this problem.

    Thanks,
    Novice_User
    Last edited by Vb_Novice_User; Jan 21st, 2010 at 12:54 PM.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Object Disposed Exception - Multiple Forms

    Is there anything special with Form3 Finalizer?
    Your code works fine with an empty form3 (by the way, is it a typo? You use the name Form3, not Form2 in your code).

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2010
    Posts
    26

    Re: Object Disposed Exception - Multiple Forms

    I don't know why it does not work for me

    I tried the same code on a fresh app, it still throws in the same error.

    Initially I had only form2. I still have it as a backup, but it is not linked to form1. My form2 and form3 have the same code. I am just keeping form2 in case I goof up on form3 (like accidentally delete something).

    Thanks,

    N_U

  4. #4

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2010
    Posts
    26

    Re: Object Disposed Exception - Multiple Forms

    @ myForm.Show()

    See attached file.

    Thanks,
    N_U
    Attached Images Attached Images  

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Object Disposed Exception - Multiple Forms

    Weird.
    do you have anything in the Form3 Sub Form_Load or Sub New ?

    This code works perfectly.

  7. #7
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Object Disposed Exception - Multiple Forms

    Quote Originally Posted by cicatrix View Post
    Weird.
    do you have anything in the Form3 Sub Form_Load or Sub New ?

    This code works perfectly.
    It's probably him loading it twice. It works at first, then fails the second time. The reason is because he's declaring the variable in the Class scope. Then he opens the form. Once the form is closed, the variable is disposed. He either has to use the default instance of the new form or call the variable declaration within the sub scope.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2010
    Posts
    26

    Resolved Re: Object Disposed Exception - Multiple Forms

    Quote Originally Posted by weirddemon View Post
    ..... He either has to use the default instance of the new form or call the variable declaration within the sub scope.
    It Works!! Thanks guys

  9. #9
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Object Disposed Exception - Multiple Forms

    Quote Originally Posted by Vb_Novice_User View Post
    It Works!! Thanks guys
    Great. Please remember to mark the thread resolved by selecting Resolved from Thread Tools near the top.

    Also, if you're declaring the variable within the sub scope, each time you close the form you're disposing of that variable. So each time you open the form, you're using a new instance of that form. Which means anything you change on that form won't stay. That is, of course, unless you're using My.Settings to save the information.

    If you're declaring the variable within the sub, then you probably won't need the disposed check.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  10. #10
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Object Disposed Exception - Multiple Forms

    Quote Originally Posted by weirddemon View Post
    Great. Please remember to mark the thread resolved by selecting Resolved from Thread Tools near the top.
    I still don't understan why this code works for me? I created a new project, then create two forms and copypasted that code. It worked Each time I pressed a button Form3 appeared again.

  11. #11
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Object Disposed Exception - Multiple Forms

    Because myForm wasn't Nothing.

    Here's what's happening:

    Dim myForm As Form3 (it is now Nothing)
    First button click...
    myForm Is Nothing so myForm = New Form3 (since it was "nothing", it will now be set to a new instance of Form3)
    myForm.Show (Show the form)

    Close the form.
    myForm is STILL an instance of Form3, but it has been Disposed. It will not be set to Nothing until AFTER the garbage collect cleans it out.

    Click button again...
    myForm doesn't change since it is NOT NOTHING.
    myForm.Show (ERROR... because you can't call Show on a Disposed form object.)
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  12. #12

  13. #13
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] Object Disposed Exception - Multiple Forms

    Garbage collect can strike at any time. Sometimes immediate, sometimes hours. It's controlled by the framework itself.

    Are you sure you copied his code exactly? It crashes for me. Open a form, close the form, try again-> crash.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  14. #14
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: [RESOLVED] Object Disposed Exception - Multiple Forms

    Bah, my instincts failed me this time:

    This is what I did 'wrong' Bad habit I picked somewhere:
    Dim myForm As Form3 = Nothing

    That's why my code was working.

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