ivansafrin/Polycode — pull request #148 by mcclure (2013-01-31 09:19:37+00:00)
Title: PolySound currently leaks OpenAL buffers, eventually breaking OpenAL completely

Currently, when you create a Sound, it creates a sound source and a buffer. When you delete the Sound, it deletes the sound source, but does not delete the buffer. Besides creating an apparent memory leak, this leads to a rather VERY problem: If you create and destroy a sufficient number of buffers (say, a couple hundred or so in my testing on mac), OpenAL *loses the ability to create new buffers* and all new sounds are created mute. I have a program which destroys and creates 14 sounds every time you press a button, so this hit me pretty hard...

This is a bit of a long patch, I cleaned up some confusingly named variables and also added a bunch of new checks for OpenAL errors. However really the only important line in the patch is:

alDeleteBuffers(1, &buffer);

You could make this line work without keeping around the buffer variable by calling

alGetSourcei(source, AL_BUFFER, &buffer);

But I went this way in case for some reason at some point one wanted to share a buffer between multiple Sounds, this way a Sound is guaranteed to delete only the buffer it created. I think either approach is probably workable.