OpenAL Memory Leak

BlitzMax Forums/BlitzMax Bug Reports/OpenAL Memory Leak

I, like many others, am using OpenAL as my sound driver in Vista due to FreeSound having a 1 second delay and DirectSound being a bit noisy/screwy.

*Can someone else please help me to verify this bug?*

Anyway, I noticed that my game was eating up the memory in Task Manager (all my framework games do on XP and Vista), even though the GC is correct. When I switch to FreeAudio they are totally fine, no leak.

I've spent ages trying to duplicate the issue in a small app, and I can only duplicate when debugging for some reason. I think it's a GC issue with OpenAL. I really wish I could pinpoint it more but I've spent a day on it and I'm getting nowhere, hopefully BRL can figure it out because they can get into the OpenAL modules and check stuff out.

Check out the following code: You need a test1.ogg (say a 500K music file) and the two OpenAL files (OpenAL32.dll and wrap_oal,dll) in the same folder.

SuperStrict

Const FREE_AUDIO%=0

If FREE_AUDIO=0 Then
	EnableOpenALAudio()
	If Not SetAudioDriver("OpenAL Generic Software") Then
		Notify("Failed to set Audio Driver")
	EndIf
EndIf

AppTitle = "Open AL Bug Test"

Graphics 400,300,0

Type TGame
	Field channel:TChannel
	Field music: TSound

	Method Init()
		channel = AllocChannel()		
	End Method
	
	Method LoadMusic(name$)
		If channel<>Null And ChannelPlaying(channel) Then
			StopChannel(channel)
			DebugStop
			channel = Null
			music = Null
			GCCollect() 'The Task Manager memory drops for FreeAudio but does not for OpenAL.
			'Remake the channel because StopChannel will have killed it but left the pointer intact.
			channel = AllocChannel()	
		EndIf
		music = LoadSound(name+".ogg",SOUND_LOOP)
	End Method
	
	Method PlayMusic()
		If channel<>Null And music<>Null Then
			CueSound(music, channel)
			SetChannelVolume(channel,1)
			ResumeChannel(channel)
		EndIf
	End Method	
	
	Method ChangeScreen()	
		LoadMusic("test1")
		PlayMusic()		
	End Method
End Type

Global Game:TGame = New TGame
Game.Init()

Game.ChangeScreen()

While Not KeyHit(KEY_ESCAPE) 
	If KeyHit(KEY_SPACE) Then
		Game.ChangeScreen()
	EndIf
	Cls
	If FREE_AUDIO=1 Then
		DrawText "Driver=FreeAudio",0,0
	Else
		DrawText "Driver=OpenAL",0,0
	EndIf
	DrawText "Press <space> to change screens.",0,20
	DrawText "GC Mem = "+GCMemAlloced(),0,40
	Flip
Wend


Directions to spot bug:

1) Run the code in DEBUG mode and then launch task manager.
2) Press Space. This will stop at the DebugStop.
3) Step to the GCCollect() line and check the memory in task manager.
4) Step again. Note how the memory is THE SAME. (If you change the FREE_AUDIO flag to 1 and do the same test, the memory will go down as expected.)
5) For ultra weirdness keep stepping until you get past the music = LoadSound line. Note how the memory goes up (as expected because it's loading in more music). Now press F5 to resume program flow. Memory remains high. This is a big memory leak. Press space again and repeat. Memory keeps going up.

HOWEVER, if you repeat the above but press F5 to resume BEFORE getting to step 5 the memory will not go up and there will be no leak! Same if you take out the DebugStop or run in non-debug mode. How weird is that?

This makes me think there's some kind of GC timing thing going on which screws up the OpenAL memory allocation in certain situations only. It's definitely screwing up all the time in my full games as I've said but I'm not doing anything different from the above code (well I'm not calling GCCollect manually, that's just to demo the bug), although I do have other sound channels on the go at the same time which may make a difference.

One last thing, I wondered if it was because of this:

When using OpenAL a TChannel is actually a TOpenALChannel which contains a _Source field of type TOpenALSource. This in turn contains a field called _Sound of type TOpenALSound which points to the sound sample. When you call StopChannel the _Source and the _Sound fields are not nulled or anything. So when the channel is nulled, I wonder if those other pointers are properly nulled (they should be right?) because if they are NOT then when you null your sound sample and expect the GC to clean it up, it WON'T because there will still be a non-null reference to it in the _Sound field! I actually tried manually nulling that field (had to modify the OpenALAudio module to make TOpenALSource public) but it didn't seem to fix the problem, so this could be a red herring...

Not seeing it here (Vista Ultimate - 64bit). Memory goes up when you load the music again, but then quickly drops back down to the previous level. Memory usage seems identical for both OpenAL and FreeAudio.

What version of OpenAL are you using?

Mine are as follows:

OpenAL32.dll 6.14.357.22
wrap_oal.dll 2.1.8.1

Some more testing. If you compile with threaded mode enable (thus using a difference garbage collector), the memory stays higher for a longer period of time (sometimes much longer) before coming back down again.

OpenAL32.dll 6.14.357.19
wrap_oal.dll 2.1.4.0

So a bit older it seems. Where did you get your version from please? I can retest with that and see if the bug still exists. [EDIT] Don't worry got it, google was my friend. Testing now...

Tried the new OpenAL. Same memory leak in my game and in the test code above.

Muttley: Did you try stepping through in debug mode like I suggested? That's the only way to see the bug.

Yep, I stepped through as directed. I'm just not seeing it.

Muttley

OK thanks, weird. I've got the memory leak on XP and Vista so I know it's not just my PC.

Just tested it at home on a 3rd PC (XP Home SP2). Same results.

Hi grey odd results here intial startup running about 16Mb in task manager, then after a few hits of the space bar memory seemed to peak/steady around 17Mb then went haywire once focus was lost currently around the 20Mb mark!

I'm wondering if OpenAL is caching or increasing it's internal cache/buffer size?

Found this snippit on the OpenAL site:
On the Windows platform, Creative Labs provides three different OpenAL playback devices. Firstly, there is a native device that ships with soundcards such as the Sound Blaster Audigy® and Sound Blaster X-Fi™ series of cards. The native device communicates directly with the drivers of the soundcard and offers the best performance, quality and feature set of all the OpenAL devices. Secondly, there is a device known as Generic Hardware which uses DirectSound 3D hardware buffers to enable hardware acceleration of OpenAL on soundcards that don't have their own native devices. Finally, there is a Generic Software device that uses a built-in software mixer to output a single audio stream to a DirectSound Buffer.


So this might depend upon the OpenAL playback device and it's use of buffers!

My game launched on BFG last night with this bug on it and is being played by 100,000s of people right now. It would be really neat if this bug could be looked into soon so I can release an update :-) Any thoughts from BRL on this? Thanks!

I thought you said it only occured when stepping through the code in debug mode? How many of your end users are going to be doing that?!

So Grey, is your new game Shop N Spree?

So Grey, is your new game Shop N Spree?


Apparently it's "Unwell Mel"

I thought you said it only occured when stepping through the code in debug mode? How many of your end users are going to be doing that?!
In my games the memory leak occurs all the time with OpenAL but not FreeAudio and I'm using the same BlitzMax commands for both which is why I know it's an internal Blitz/OpenAL issue (tested on multiple PCs). But I can't seem to duplicate that with a reduced down code sample unless I use the debug mode stepping through technique shown above. I had to post something that demos the problem in order to get this bug noticed and fixed by BRL (I hope).

@TaskMaster: I don't want to derail this thread as I *really* want to get this bug fixed, but yeah as xlsior says, my new game is Unwell Mel. If it was Shop N Spree I'd be equally pleased because it's awesome (their last game, Fast Class Flurry was a fantastic piece of work too).

. *Edited unrelated nonsense* ;)

Hi REDi, but Mark said he fixed it, maybe he didn't or made a new bug? Or this is totally different...

Hello BRL, I'm wondering if you have any thoughts about fixing this issue? I'll probably release an updated version of my game in a couple of weeks so it would be really great if the OpenAL issue could be resolved soon. The game is doing great btw, it's no. 2 on BFG top 10 right now and looks to be the best selling match-3 of the last few years. Another good BMax success story. Thanks! :-)

Aside from the fact that this is a serious problem (I still going to check it out later, now that I bought max), why dont you use Directsound on Vista? Only Freeaudio shows the "lag" here, Directsound works perfectly (tested on Vista 64)

Directsound used to be crackly/fuzzy on some Vista setups. I don't know if it still is.

Yeah Directsound is a OK alternative, but I've found it to be overly noisy + have the odd crackle and other anomalies. OpenAL sounds a lot nicer and smoother.

Hi,

Memory levels out at about 14M here (using bouncy.ogg in samples/fredborg/oldskool2). Even changing MouseHit to MouseDown and really thrashing it with a shorter sample doesn't 'leak' or anything.

The fact the task manager does not show a memory drop when you gccollect is no biggy - OpenAL may preallocate buffers, cache stuff etc. Hell, it may even use the Hans Boehm GC for all I know, which would likely give you very odd memory usage in task manager! And it may, of course, have a leak that only shows up when a certain code path is taken on certain hardware...

Stepping through an app line by line and watching memory usage wont tell you a hell of a lot, because you have no idea how the various libraries involved are managing their memory.

As I've tried to stress before, the important think is 'does memory usage level out after a while' and, for me at least (and apparently others), it does.

Thanks for checking this Mark.

I'm seeing the memory go up loads on my games when run with Open AL on all 3 PCs I've tested it on (two XP and 1 Vista - totally different hardware, oh and a BFG QA Vista machine too). It doesn't happen with FreeAudio.

It's hard to duplicate my game code for some reason but when the code sample above is stepped through and resumed as per the instructions, the memory *stays* high. Each time it's stepped through it can be forced higher and higher and it never drops down. BUT ONLY if you follow step 5 in my instructions, step 4 won't make it happen. I've tested that on more than one PC too.

I'd love it if some more people could verify what is happening when the 5 steps are followed.

Sorry to double check you Mark, but did you follow through to step 5 then repeat? This should boost up the memory level and never drop it. If you did that and did not see the error then I guess I'll have to make some code that has the memory leak when running normally (i.e. not stepping in debug mode). I've got no hope of fixing it but I'm thinking that if you can see the error at your end you may be able to trace it more effectively and find a workaround.

I really believe this is worth solving because otherwise Vista users will experience slow downs and eventually a crash for games that load and free a lot of music with OpenAL (like mine).

I get the memory leak here, too. Memory usage is 25MB at first run but after a few repetitions of space/step it ended up well over 80MB and would have kept going up further if I could've been bothered to do it over and over some more. When I let the program run again, it never dropped back to 25MB.

openal32.dll: 6.14.0357.22
wrap_oal.dll: 2.1.8.1

Test system: Compaq Presario C700, Pentium Dual Core 2x1.4GHz, 2GB RAM, Conexant HD-Audio, Vista Home Premium SP1 (32bit).

Its possibly worth noting: If I run in release mode, or simply comment out the DebugStop, GCCollect() functions as expected and the memory usage does not increase. It also works normally if I just hit "step out" a few times until the program runs again.

So it seems that garbage collection may have a slight issue when stepping with the debugger.

Thanks for confirming my test. Yes it seems that there is a garbage collection issue with OpenAL (it doesn't do it with FreeAudio) when stepping in debug mode. However I believe that it is linked to a larger issue which I've seen when my game is running in release mode (but I don't seem to be able to write some sample code to 100% duplicate it). I was hoping that Mark might figure out what is going on in debug mode and go, "hang on a minute, it shouldn't be doing that" and then fix it and thus my release code problem would go away too. :-)