BlitzMax app crashes and I don't know why

BlitzMax Forums/BlitzMax Programming/BlitzMax app crashes and I don't know why

My BlitzMax app crashes and I don't know why. The flowing function from my type TPlayer contains the bug.

Function createPlayer:TPlayer()
	
	Local player:TPlayer = New TPlayer
	
	player.x	= MouseX()
	player.y	= MouseY()
	
	Local point1:TPoint	= TPoint.createPoint(player.SIZE / 2, 0)
	Local point2:TPoint	= TPoint.createPoint(player.SIZE, player.SIZE)
	Local point3:TPoint	= TPoint.createPoint(0, player.SIZE)
	
	drawTriangle(point1, point2, point3)
	
	player.image = CreateImage(player.SIZE, player.SIZE)
	
	GrabImage(player.image, 0, 0)
	
	Return player
EndFunction


When I comment out the lines from drawTriangle... to Return it works. Two hours past and still I didn't find the bug.


Thanks, Nicolas.

Does player.SIZE have a value of 0 by chance?

No, it haze a value of 128.


Nicolas.

If it has a value of 128, where is it declared in this function.
I mean something like:

player.size = 128

klepto,
I guess he sets this during the creation of the TPlayer:
Type TPlayer
 Field size=128
End Type
Is player.image a TImage?
Maybe the bug is in the drawtriangle funtion(?)

That's correct jb, it's a Constant. Yes player.image is a TImage and no drawTriangle works perfect. Maby this helps:

tPoint.bmx
Strict

Framework BRL.GLMax2D
Import BRL.Basic
Import BRL.System

Type TPoint
	
	Field x:Int
	Field y:Int
	
	'** createPoint:TPoint(x:Int, y:Int) **************************************************************
		
		Function createPoint:TPoint(x:Int, y:Int)
			
			Local point:TPoint = New TPoint
			
			point.x	= x
			point.y	= y
			
			Return point
		EndFunction
	'** End createPoint:TPoint(x:Int, y:Int) **********************************************************
EndType

'** drawTriangle(point1:TPoint, point2:TPoint, point3:TPoint) **************************************
	
	Function drawTriangle(point1:TPoint, point2:TPoint, point3:TPoint)
		
		Cls
		
		DrawLine point1.x, point1.y, point2.x, point2.y
		DrawLine point2.x, point2.y, point3.x, point3.y
		DrawLine point3.x, point3.y, point1.x, point1.y
	EndFunction
'** End drawTriangle(point1:TPoint, point2:TPoint, point3:TPoint) **********************************


tPlayer.bmx
Import "tPoint.bmx"

Type TPlayer
	
	Const SIZE:Int = 128
	
	Field x:Int
	Field y:Int
	Field image:TImage
	
	'** createPlayer:TPlayer() ************************************************************************
		
		Function createPlayer:TPlayer()
			
			Local player:TPlayer = New TPlayer
			
			player.x	= MouseX()
			player.y	= MouseY()
			
			Local point1:TPoint	= TPoint.createPoint(player.SIZE / 2, 0)
			Local point2:TPoint	= TPoint.createPoint(player.SIZE, player.SIZE)
			Local point3:TPoint	= TPoint.createPoint(0, player.SIZE)
			
			'>>> drawTriangle(point1, point2, point3)
			'>>>
			'>>> player.image = CreateImage(player.SIZE, player.SIZE)
			'>>>
			'>>> GrabImage(player.image, 0, 0)
			'>>>
			Return player
		EndFunction
	'** End createPlayer:TPlayer() ********************************************************************
	
	'** draw() ****************************************************************************************
		
		Method draw()
			
			DrawImage image, x, y
			
			update()
		EndMethod
	'** End draw() ************************************************************************************
	
	'** update() **************************************************************************************
		
		Method update()
			
			x	= MouseX()
			y	= MouseY()
		EndMethod
	'** End update() **********************************************************************************
EndType



Nicolas.

Would the crashlog be of any use?


Nicolas.

What happens if you leave just one of those lines in? For example, does it crash if you just uncomment DrawTriangle?

Yes, when I uncomment the drawTriangle it crashes but I'm certain that's not the problem because it works in other parts of my program.


Nicolas.

What happens if you do it the other way around, ie. comment out DrawTriangle and enable the other two lines?

Do you have the debugger on? Do you get any error messages if so?

Shouldn't this image be created with the DYNAMICIMAGE flag since you're using grabimage?
	player.image = CreateImage(player.SIZE, player.SIZE)


If I'm not mistaking if you create an image whit CreateImage it's always combined whit DYNAMICIMAGE. Anyway I tried it and sadly it doesn't work :(.


Nicolas.

This works (albeit the triangle doesn't draw properly).
Graphics 800,600,0

Type TPoint
	
	Field x:Int
	Field y:Int
	
	'** createPoint:TPoint(x:Int, y:Int) **************************************************************
		
		Function createPoint:TPoint(x:Int, y:Int)
			
			Local point:TPoint = New TPoint
			
			point.x	= x
			point.y	= y
			
			Return point
		EndFunction
	'** End createPoint:TPoint(x:Int, y:Int) **********************************************************
EndType

'** drawTriangle(point1:TPoint, point2:TPoint, point3:TPoint) **************************************
	
	Function drawTriangle(point1:TPoint, point2:TPoint, point3:TPoint)
		
		Cls
		
		DrawLine point1.x, point1.y, point2.x, point2.y
		DrawLine point2.x, point2.y, point3.x, point3.y
		DrawLine point3.x, point3.y, point1.x, point1.y
	EndFunction
'** End drawTriangle(point1:TPoint, point2:TPoint, point3:TPoint) **********************************


Type TPlayer
	
	Const SIZE:Int = 128
	
	Field x:Int
	Field y:Int
	Field image:TImage
	
	'** createPlayer:TPlayer() ************************************************************************
		
		Function createPlayer:TPlayer()
			
			Local player:TPlayer = New TPlayer
			
			player.x	= MouseX()
			player.y	= MouseY()
			
			Local point1:TPoint	= TPoint.createPoint(player.SIZE / 2, 0)
			Local point2:TPoint	= TPoint.createPoint(player.SIZE, player.SIZE)
			Local point3:TPoint	= TPoint.createPoint(0, player.SIZE)
			
			drawTriangle(point1, point2, point3)
			
			player.image = CreateImage(player.SIZE, player.SIZE)
			GrabImage(player.image, 0, 0)
			
			Return player
		EndFunction
	'** End createPlayer:TPlayer() ********************************************************************
	
	'** draw() ****************************************************************************************
		
		Method draw()
			
			DrawImage image, x, y
			
			update()
		EndMethod
	'** End draw() ************************************************************************************
	
	'** update() **************************************************************************************
		
		Method update()
			
			x	= MouseX()
			y	= MouseY()
		EndMethod
	'** End update() **********************************************************************************
EndType

Local myPlayer:TPlayer = TPlayer.createPlayer()


While Not KeyDown(KEY_ESCAPE)
myPlayer.Draw()
Flip
Cls
Wend


Yes it works. Hmmm strange. What am I doing different?

By the way to get the triangle to draw properly:
Local point1:TPoint	= TPoint.createPoint(player.SIZE / 2, 0)
Local point2:TPoint	= TPoint.createPoint(player.SIZE - 1, player.SIZE - 1)
Local point3:TPoint	= TPoint.createPoint(0, player.SIZE - 1)

But you already knew that ;).


Nicolas.