Start New Discussion within our Software Development Community

Hello Forum I'm learning more about Form Apps in 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()

        '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)


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

        'DRAW TOON
        GO.DrawImage(BMPToon, XPos * TileSize * MoveSpeed, YPos * TileSize * MoveSpeed, SRect, GraphicsUnit.Pixel)

        '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)

        GO = Graphics.FromImage(BB)


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


    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.

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.

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.

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?

Yeah the line of code that was triggering the error was BBGO = Me.Create Graphics and I used:
BBGO. Me.CreateGraphics
BBGO.DrawImage(BB, 0, 0, ResWidth, ResHeight)
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!

This question has already been answered. Start a new discussion instead.