Flushmem a pain in the RRR

BlitzMax Forums/BlitzMax Programming/Flushmem a pain in the RRR

Am I the only one who's finding flushmem irritating?

I have no problem when I just have to stick a flushmem in my main loop, but recently its been causing me headaches. Mucking about with FMOD and the sound kept going jittery - because I forgot to put a flushmem in the callback. This took a day to figure out. Then I decided to stick some graphics in my program, so I put in the standard main loop with a flushmem. *CRASH* without an error report. Took another day to realise it might be because I have 2 flushmems - one in the main loop and one in the FMOD callback. Indeed this was the problem, but should this really cause a crash? Bring back good old delete - it was a lot less hastle!

Mark has already stated that the next update will have some changes to the FlushMem system.

Am I the only one who's finding flushmem irritating?
Yes.

So you're reading in everyone's mind FlameDuck?
That's cool, I'd like to.

That's cool, I'd like to.
Yeah, but you can't, since you don't have super powers.

Use your amazing powers to get this working then, Superduck. I'm sure it's a flushmem() problem.

Framework brl.glmax2d
'----------------------
Import brl.retro
Import brl.blitz
Import brl.freeaudioaudio
'Import brl.oggloader   'causes probs with fmod on mac, thats why I had to use framework. Any help?
Import brl.system	
Import pub.fmod
Import brl.keycodes
Import brl.standardio

Global slider_list:TList=CreateList()

sample_rate=44100
dt#=1.0/Float(sample_rate)

Type toneinfo
	Field amp# 'Max 32767
	Field dphase#
	Field phase#=0.0
	
	Function create:toneinfo(freq#,amp#,dt#,chan:String)
		temp:toneinfo=New toneinfo
		temp.dphase=360.0*freq*dt
		temp.amp=amp
		Select chan
			Case "l"
				DebugLog "creating tone on left channel"
				l_tone_list.addlast(temp)

			Case "r"
				DebugLog "creating tone on right channel"
				r_tone_list.addlast(temp)
			Default
				DebugLog "no channel assigned"
				temp=Null
		EndSelect
		
		Return temp
	End Function
End Type

Global l_tone_list:TList=CreateList()
Global r_tone_list:TList=CreateList()

Function makesignedshort:Short(float_num#)
	x=Long(float_num)
	val_bits:Short=Short(x&%0111111111111111)
	sign_bit:Short=Short((x Shr 16)&%1000000000000000)
	short_val:Short=Short(sign_bit|val_bits)
	Return short_val
End Function
	
Function FMOD_Stream_Callback(stream:Byte Ptr, buff:Byte Ptr, length:Int, userdata:Byte Ptr)
	
	
	Local snd_ptr:Short Ptr=Short Ptr(buff)
		
	For n=0 To (length/4)-1
	
		l_sound#=0.0
		r_sound#=0.0
		
		For l_tone:toneinfo=EachIn l_tone_list 
			l_sound:+l_tone.amp*Sin(l_tone.phase)
			l_tone.phase:+l_tone.dphase
		Next
		
		For r_tone:toneinfo=EachIn r_tone_list 
			r_sound:+r_tone.amp*Sin(r_tone.phase)
			r_tone.phase:+r_tone.dphase
		Next								
																																																																																									
		snd_ptr[0]=makesignedshort(l_sound)		'Left speaker
		snd_ptr[1]=makesignedshort(r_sound)		'Right speaker
		snd_ptr:+2

	Next
	
	l_tone.phase:Mod 360.0
	r_tone.phase:Mod 360.0	
	
	FlushMem()	
	
	Return 1
End Function

Local CallbackPtr:Byte Ptr = FMOD_Stream_Callback

Graphics 800,600

?Mac
FSOUND_SetOutput(FSOUND_OUTPUT_MAC)
?

FSOUND_SetDriver(0)
If Not FSOUND_Init(44100, 16, 0)
	Print "error"
EndIf

toneinfo.create(100.0,32000.0,dt,"l")
toneinfo.create(107.83,32000.0,dt,"r")

Local stream:Int = FSOUND_Stream_Create(CallbackPtr, 6*2048, FSOUND_NORMAL|FSOUND_16BITS|FSOUND_STEREO, 44100, Null)
FSOUND_Stream_Play(FSOUND_FREE, stream)

If (FSOUND_Stream_Play(FSOUND_FREE, NSFStream) = -1)
	Print("Error!")
EndIf

Tslider.create(10,100,600,20,0,100,50)
While Not KeyHit(KEY_ESCAPE)
	Cls	
	Tslider.draw_all()
	FlushMem()
	Flip()
Wend

FSOUND_Stream_Close(stream)
FSOUND_Close()

End

Type Tslider
	Field x#,y#
	Field width#,height#
	Field minval#,maxval#
	Field val#
	Field colr#=200,colg#=200,colb#=200
	
	Function create:Tslider(x#,y#,width#,height#,minval#,maxval#,startval#)
		temp:Tslider=New Tslider
		temp.x=x
		temp.y=y
		temp.width=width
		temp.height=height
		temp.minval=minval
		temp.maxval=maxval
		temp.val=startval
		ListAddLast(slider_list,temp)
		Return temp
	End Function
	
	Function draw_all()
		For s:Tslider=EachIn slider_list
			SetColor s.colr,s.colg,s.colb
			DrawRect s.x,s.y,s.width,s.height
			SetColor 1.0,1.0,1.0
			DrawRect s.pos_at_val(s.val),s.y,1.0,s.height 
		Next
	End Function
	
	Method pos_at_val(value#)
		pos#=Self.x+((value-Self.minval)/(Self.maxval-Self.minval))*Self.width
		Return pos
	End Method
	
End Type
'No


Simple explanation: FlushMem isn't the problem, you are.

Thanks a lot, big nose ; )

Seriously though, I think I'm seeing a bug in Max.

There's a lot of attitude from you noel, I don't think it should continue unchecked. Please think then post.

FlushMem's automated in the next version (though a find/replace will let you continue to use it manually if you prefer).

Use your amazing powers to get this working then, Superduck. I'm sure it's a flushmem() problem.
Don't have pub.fmod. Sorry.

I think some people are missing the point - this is not a thread about my personal preferences on garbage collection, I'm saying there's a bug that's causing an un-handled crash and asking if anyone can see a problem with the way I'm using Flushmem.

I WOULD REALLY APPRECIATE SOME HELP - EVEN IF ITS JUST A SCAN THROUGH MY CODE BY EYE, not running it.

All the FMOD stuff works fine until I put a main loop in with a flushmem(). You can ignore the slider stuff - it crashes without this.

pub.fmod is here if anyone is interested:

http://www.blitzmax.com/Community/posts.php?topic=49584