newbie, error with my code

BlitzMax Forums/BlitzMax Beginners Area/newbie, error with my code

hub(Posted 2005) [#1]
Hi !

The next code produce an error : ''unhandled exception:Unhandled memory exception error"

but i don't understand why ! could you help me please !

Thanks !


Type Player

	Field x

	Function  Create:Player(FileName$)
		r:Player = New Player
		r.x = 1
		Return r
	End Function
	
End Type

Local p:Player
p = p.Create ("laser.png") ' <- error !



Matt McFarland(Posted 2005) [#2]
hmm it cant find laser.png (sounds like to me)

I get the same error whenever it cant find a file im trying to load.


Matt McFarland(Posted 2005) [#3]
OH yeah, are you loading teh image over and over again in a loop? if you do that you'll cause a huuuge memory leak..


Chris C(Posted 2005) [#4]
local p:player=new player

matts right you should only load the image once, you can always pass a reference to the image for your create code...


hub(Posted 2005) [#5]
.


Eric(Posted 2005) [#6]
p = player.Create ("laser.png")


Matt McFarland(Posted 2005) [#7]
Eric, we already said that if he creates lasers (and loads the png file) everytime that he'll have a huuuuge memory leak!! Why help him break it? :P


Eric(Posted 2005) [#8]
Well.. As I read the code there is no reference to loading the actual image.. I was only trying to get rid of the Error for him. :)


hub(Posted 2005) [#9]
Thanks !
with blitzplus you use copyimage. What's the method with bmax ?


Matt McFarland(Posted 2005) [#10]
ummm, not sure if this is right...

image:timage VariableName = loadimage "blah.blah"

err is it variablename:timage = image:timage("blah.blah")

hmmm cant tell off the top of my head.

maybe it is
ImageName:Timage = LoadImage:Timage ("blah.png")

Then to draw it:

DrawImage(ImageName,x,y)

Sorry! I'm at work... :|


Brucey(Posted 2005) [#11]
According to the code-snippet, Eric has the correct fix.
You can't do
p = p.Create ("laser.png")

because the variable p has not be instantiated yet, and so is null.
That is why you must use
p = Player.Create("laser.png")

Everyone else appears to have been distracted by the fact that he was passing in a string for a file, which was not the cause of the error (even if, off topic, it wasn't a good idea in the future when the code was completed, to be loading an image for each new Player object)

;-)


hub(Posted 2005) [#12]
Thanks. I've modified the code now. This was an example. I agree that is a bad idea to use loadimage for each player !