We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,878 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Problem with 'System.ObjectDisposedException'

Hello Forum I'm learning more about Form Apps in VB.net and I encountered the error 'System.ObjectDisposedException', no matter what I do this error doesn't seem to want to stop.
This error occurs in two stages all under the declaration of BBGO which is declared as graphics.
This should be the relevent code:

Private Sub DrawGraphics()

        'FILL THE BACKBUFFER
        'DRAW TILES
        For X = 0 To 38
            For Y = 0 To 28

                GetSourceRect(X, Y, TileSize, TileSize)
                DRect = New Rectangle(X * TileSize, Y * TileSize, TileSize, TileSize)
                GO.DrawImage(Wall, DRect, SRect, GraphicsUnit.Pixel)

            Next
        Next

        GO.DrawRectangle(Pens.Red, mouseX * TileSize, mouseY * TileSize, TileSize, TileSize)

        'DRAW TOON
        GetSprite(LastDir)
        BMPToon.MakeTransparent(Color.Black)
        GO.DrawImage(BMPToon, XPos * TileSize * MoveSpeed, YPos * TileSize * MoveSpeed, SRect, GraphicsUnit.Pixel)

        'DRAW FINAL LAYERS
        'GUYS, MENUS, ETC

        GO.DrawString("Controls: " & vbCrLf & _
                      "W = Up" & vbCrLf & _
                      "S = Down" & vbCrLf & _
                      "A = Left" & vbCrLf & _
                      "D = Right" & vbCrLf & _
                      "Space = Stop" & vbCrLf & _
                      "", Me.Font, Brushes.Red, 625, 0)

        'COPY BACKBUFFER TO GRAPHICS OBJECT
        GO = Graphics.FromImage(BB)

        'DRAW BACK BUFFER TO SCREEN

        ***BBGO = Me.CreateGraphics
        BBGO.DrawImage(BB, 0, 0, ResWidth, ResHeight)***

        'FIX OVERDRAW
        GO.Clear(Color.Black)

    End Sub

There is alot more code to this form but I dont want to go over board with it all if you guys need more of the code or any help I'll give it to you but in the mean time I'm going to keep pounding at this error.

2
Contributors
5
Replies
2 Days
Discussion Span
5 Months Ago
Last Updated
6
Views
Question
Answered
Blaze_Razgriz
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Run your application in the debugger and step through everything until you know exactly what line throws the exception.

Per MSDN library the 'System.ObjectDisposedException' is defined as:
The exception that is thrown when an operation is performed on a disposed object.

john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25
Solved Threads: 22
Skill Endorsements: 3

It's at draw back buffer to screen and occurs when I close the application. The actual code it points to is BBGO = Me.CreateGraphics. I am using the Try Catch method to solve the problem and it seems to be working. Perhaps not the best way to make a game app.

Blaze_Razgriz
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

MSDN documentation for that exception states that it is an exception thrown when an operation is performed on an already disposed object.

A lot of code executes when a form is closed, were you able to trace it to an exact line of code?

john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25
Solved Threads: 22
Skill Endorsements: 3

Yeah the line of code that was triggering the error was BBGO = Me.Create Graphics and I used:
Try
BBGO. Me.CreateGraphics
BBGO.DrawImage(BB, 0, 0, ResWidth, ResHeight)
Catch
End
End Try
To catch it and if it does catch it it ends the whole class...which is kind of what I wanted to do when I exited the program, There were also a few other errors i had to catch because they were not being disposed and I had to dispose of them myself and catch the error if they tried to run after they were disposed. Sorry for causing such a fuss.

I bet there is a better way to make 2D games without XNA but I'll get there eventually.

Thanks a lot for your time!

Blaze_Razgriz
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

There probably is a better way... I don't do drawing code though, so I wouldn't be much help there.

john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25
Solved Threads: 22
Skill Endorsements: 3
Question Answered as of 5 Months Ago by john.knapp

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0834 seconds using 2.7MB