C++

Miscellaneous Forums/General Discussion/C++

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

Question:

When converting to an array, I have an error with deleting the particle... does anyone know why?

'TParticle type, basicly a wrapper w/ drawing functions for the class counterpart.
Const MAX_PARTICLES:Int = 10000
Global ParticleList:TParticle[MAX_PARTICLES]
Global pCount:Int = 0


Type TParticle
	Field img:TImage
	Field cBuddy:Byte Ptr
	
	Method Create:TParticle(_x:Float, _y:Float, _img:TImage, _dir:Int = 0, _speed:Float = 0, frames:Int = 1, _fade:Int = False, _autorot:Int = False, _z:Int = 0)
		'create a "c" particle and give it's handle to the cbuddy variable
		cBuddy = cCreateParticle(_x, _y, _dir, _speed, frames, _fade, _autorot, _z)
		'Add given image to the object
		img = _img
		'Add this object to the particle list
		ParticleList[pCount] = Self
		pCount:+1
	End Method
	
	Method Update()
		'Update the "c" particle
		cUpdateParticle(cBuddy)
		'Grab the new x/y positions of the "c" particle(so we only have to use this function once per loop)
		Local x:Float = cPX(cBuddy)
		Local y:Float = cPY(cBuddy)
		
		If cPLife(cBuddy) = 0 Or x > GraphicsWidth()+ImageWidth(img) Or x < 0-ImageWidth(img) Or y > GraphicsHeight()+ImageHeight(img) Or y < 0-ImageHeight(img)
			Destroy()
			Return
		End If
		
		'Draw the given img at the "c" Particle's locations 
		SetAlpha cPA(cBuddy)
		SetRotation cPR(cBuddy)
		DrawImage img,x,y
		SetAlpha 1
		SetRotation 0
	End Method
	
	Method Destroy()
		cDeleteParticle(cBuddy)
		ParticleList[pCount-1] = Self
		pCount:-1
	End Method
	
End Type


It's giving me a memory error on the cDeleteParticle command, probably because the copied particle's cBuddy pointer is already deleted...

How do I check if the pointer is deleted or not? I tried creating a "dead" field and just turning it true once the deletion process was done, then just check that, and if it's not dead then delete the cBuddy pointer. But now it just crashes without an error when a particle is destroyed.

here's that code:
'TParticle type, basicly a wrapper w/ drawing functions for the class counterpart.
Const MAX_PARTICLES:Int = 10000
Global ParticleList:TParticle[MAX_PARTICLES]
Global pCount:Int = 0


Type TParticle
	Field img:TImage
	Field cBuddy:Byte Ptr
	Field dead:Int
	
	Method Create:TParticle(_x:Float, _y:Float, _img:TImage, _dir:Int = 0, _speed:Float = 0, frames:Int = 1, _fade:Int = False, _autorot:Int = False, _z:Int = 0)
		'create a "c" particle and give it's handle to the cbuddy variable
		cBuddy = cCreateParticle(_x, _y, _dir, _speed, frames, _fade, _autorot, _z)
		'Add given image to the object
		img = _img
		'Add this object to the particle list
		ParticleList[pCount] = Self
		pCount:+1
	End Method
	
	Method Update()
		'Update the "c" particle
		cUpdateParticle(cBuddy)
		'Grab the new x/y positions of the "c" particle(so we only have to use this function once per loop)
		Local x:Float = cPX(cBuddy)
		Local y:Float = cPY(cBuddy)
		
		If cPLife(cBuddy) = 0 Or x > GraphicsWidth()+ImageWidth(img) Or x < 0-ImageWidth(img) Or y > GraphicsHeight()+ImageHeight(img) Or y < 0-ImageHeight(img)
			Destroy()
			Return
		End If
		
		'Draw the given img at the "c" Particle's locations 
		SetAlpha cPA(cBuddy)
		SetRotation cPR(cBuddy)
		DrawImage img,x,y
		SetAlpha 1
		SetRotation 0
	End Method
	
	Method Destroy()
		If Not dead cDeleteParticle(cBuddy)
		ParticleList[pCount-1] = Self
		pCount:-1
		dead = True
	End Method
	
End Type


the C++ code is the same