i've recently started learning OpenAL. for the most part it's been
smooth going. however, i'm now having a problem removing an OpenAL
object from an array if the source is currently playing. doing so
crashes my app after deallocating the object. removing a sound while
it's not playing works fine.
subsequently, my immediate course of action was to query
AL_SOURCE_STATE before removing the sound. if the source is playing,
stop the sound (alSourceStop) and then remove the object.
surprisingly, this didn't work and my app still crashed after
deallocation. some of the sounds i plan on using are for background
music, so they will be looping and quite lengthy compared to a short
sound effect. therefore, querying the source's state before removing
the sound only when the source is finished wouldn't really be an
option even if it did work.
what does seem to work is setting a timer to remove the sound sometime
after the sound has stopped, although following this path would be insecure
and problematic in the long run.
smooth going. however, i'm now having a problem removing an OpenAL
object from an array if the source is currently playing. doing so
crashes my app after deallocating the object. removing a sound while
it's not playing works fine.
subsequently, my immediate course of action was to query
AL_SOURCE_STATE before removing the sound. if the source is playing,
stop the sound (alSourceStop) and then remove the object.
surprisingly, this didn't work and my app still crashed after
deallocation. some of the sounds i plan on using are for background
music, so they will be looping and quite lengthy compared to a short
sound effect. therefore, querying the source's state before removing
the sound only when the source is finished wouldn't really be an
option even if it did work.
what does seem to work is setting a timer to remove the sound sometime
after the sound has stopped, although following this path would be insecure
and problematic in the long run.
Code:
// Controller Class
- (void)removeSoundWithID:(NSUInteger)soundID
{
NSString *soundFile = [self keyForSoundID:soundID];
if(!soundFile) return;
[soundDictionary removeObjectForKey:soundFile];
}
//OpenAL Sound Object
- (void)dealloc
{
alDeleteSources(1, &sourceID);
for (NSNumber *tmpSourceID in temporarySounds)
{
NSUInteger srcID = [tmpSourceID unsignedIntegerValue];
alDeleteSources(1, &srcID);
}
[temporarySounds release];
alDeleteBuffers(1, &bufferID);
if (bufferData)
{
free(bufferData);
bufferData = NULL;
}
[sourceFileName release];
[super dealloc];
NSLog(@"Deallocation Complete");
}