Unhandled Exception: CreateSurface failed.

BlitzMax Forums/BlitzMax Beginners Area/Unhandled Exception: CreateSurface failed.

I rearranged stuff so as to avoid having two instanced of TGraphics in existence at once, but I'm still having trouble:

'Global App:Application = Application.Create("Fruit Sliders", 800, 600)


Type Application

	Field Name$
	
	Field Width%
	Field Height%
	Field Depth%

	Field Window:TGadget
	Field Canvas:TGadget
	Field Panel:TGadget
	
	Field TG:TGraphics
	
	Field Windowed%


	' -------------------------------------------------------------------------------------------------------------------------------------------------------
	' This function creates the game window, and sets up the graphics objects for windowed and fullscreen modes. 
	' -------------------------------------------------------------------------------------------------------------------------------------------------------

		Function Create:Application(AppName$, AppWidth%, AppHeight%, AppDepth%=0, Fullscreen%=False)
	
			Local X%, Y%	
			Local App:Application
			
			'SetGraphicsDriver GLMax2DDriver()
			
			' Create new application.
				App = New Application 				
									
			' Store application settings.
				App.Name$  = AppName$
				App.Width  = AppWidth
				App.Height = AppHeight	
				App.Depth  = AppDepth
				
			' If Depth is 0, try to find a suitable fullscreen bit depth.
			
				If App.Depth = 0
				
					App.Depth = 16
					If GraphicsModeExists(App.Width, App.Height, 24) Then App.Depth = 24
					If GraphicsModeExists(App.Width, App.Height, 32) Then App.Depth = 32
				
				EndIf		
					
			' Create a window in the center of the screen.
		
				X = GadgetWidth(Desktop())/2  - App.Width/2
				Y = GadgetHeight(Desktop())/2 - App.Height/2

				App.Window = CreateWindow(App.Name$, X, Y, App.Width, App.Height, Null, WINDOW_TITLEBAR|WINDOW_RESIZABLE|WINDOW_CLIENTCOORDS|WINDOW_HIDDEN)
				SetMinWindowSize(App.Window, App.Width, App.Height)
																
			' Create a canvas in the window to draw graphics to.
				App.Canvas = CreateCanvas(0, 0, App.Width, App.Height, App.Window)
								
			' Create a panel in the window, drawn behind the canvas, which fills the empty space around the gameplay area when the window is maximized.
				App.Panel = CreatePanel(0, 0, App.Width, App.Height, App.Window)
				SetPanelColor(App.Panel, 0, 0, 0)
				SetGadgetLayout(App.Panel, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED)
		
			' Set game to windowed or fullscreen as desired.
				Select Fullscreen
					Case False App.SetWindowed()
					Case True  App.SetFullscreen()
				End Select
			
			' Return new application instance.
				Return App
												
		End Function
		

	' -------------------------------------------------------------------------------------------------------------------------------------------------------
	' This method toggles between fullscreen or windowed mode, choosing the opposite of whichever is currently enabled.
	' -------------------------------------------------------------------------------------------------------------------------------------------------------
	
		Method ToggleFullscreen()
			Select Windowed
				Case False SetWindowed()
				Case True  SetFullscreen()
			End Select
		End Method

	' -------------------------------------------------------------------------------------------------------------------------------------------------------
	' This method puts the application in windowed mode.
	' -------------------------------------------------------------------------------------------------------------------------------------------------------
		
		Method SetWindowed()
		
			' Set drawing operations to go to canvas.
				EndGraphics
				'If TG <> Null Then CloseGraphics(TG)
				DebugStop
				TG = CanvasGraphics(Canvas) 
				SetGraphics TG
				
				Windowed = True
				
			' Show window.
				ShowGadget Window
				
			' Canvas must be activated for input or else game will make user click on canvas before any polled keyboard input is returned.
				EnablePolledInput()			
													
		End Method	


	' -------------------------------------------------------------------------------------------------------------------------------------------------------
	' This method sets the application to fullscreen mode, unless no fullscreen mode in the desired resolution exists.
	' -------------------------------------------------------------------------------------------------------------------------------------------------------

		Method SetFullscreen()
			
			' Set drawing operations to fullscreen.
				EndGraphics
				'If TG <> Null Then CloseGraphics(TG)
				TG = Graphics(Width, Height, Depth)
			
			' If the operation fails, put us back in windowed mode.  Otherwise, hide the window, and record that fullscreen is enabled.
			
				If TG = Null 
					SetWindowed()
					Return
				Else
					HideGadget Window
					Windowed = False
					EnablePolledInput()
				EndIf
			
		End Method

				
End Type	


This works in OpenGL, (Not exactly lighting fast on the switch.) and in DirectX it will switch from windowed to fullscreen mode, (Much faster and nicer switch) but it won't switch back from fullscreen to windowed in DirectX mode. It fails right after the DebugStop on the "SetGraphics TG" line, with an error about trying to access a null object. I thought the Null object was TG, but that is fine. The Canvas too, still exists. So something internal to BlitzMax itself is going wrong I think.

Even weirder... When the debugger stops, check out Canvas. It has a field _graphics:TGraphics, and it is set to something, even though I've ended the graphics modes. And I tried two different ways to end the graphics mode and both failed.

Hm... Actually come to think of it, after I end the grpahics mode, I didn't set TG to Null. Maybe saving the graphics object pointer causes an issue? I don't think it is supposed to though... I think freeing it should be enough and I am not allowed to use it after that, but keeping it around hsould be fine. Oh well guess I'll test it to be sure.

[edit]
Nope, that did bupkiss.
[/edit]