i am adding sound to my iOS game using OpenAL, i downloaded the sample code "MusicCube" at Apple Developer, i use the code directly in my game and it works fine.
but however, i have a question about the sample code, in the sample code, in file MusicCubePlayback.m i find the initBuffer function, for your convenience i paste it here?
- (void) initBuffer
{ALenum error = AL_NO_ERROR;ALenum format;ALsizei size;ALsizei freq;NSBundle* bundle = [NSBundle mainBundle];// get some audio data from a wave fileCFURLRef fileURL = (CFURLRef)[[NSURL fileURLWithPath:[bundle pathForResource:@"sound" ofType:@"wav"]] retain];if (fileURL){_data = MyGetOpenALAudioData(fileURL, &size, &format, &freq);NSLog(@"audioInfo:%d,%d,%d",size,format,freq);CFRelease(fileURL);if((error = alGetError()) != AL_NO_ERROR) {printf("error loading sound: %x\n", error);exit(1);}// use the static buffer data APIalBufferDataStaticProc(_buffer, format, _data, size, freq);if((error = alGetError()) != AL_NO_ERROR) {printf("error attaching audio to buffer: %x\n", error);}}else{printf("Could not find file!\n");_data = NULL;}}
my question is: after the alBuffer established, why didn't they free the _data? i think the alBuffer should be a memory in audio card and through the initBuffer function we submit the audio data in _data to audio card(alBuffer) and then _data is no more needed so we can free it. but i tried to free _data and it cause the sound not play normal.
i just wonder why it is not just as the process of creating textures in OpenGL: when creating texture, after the texture established we can free the image data.
so, back to OpenAL and the MusicCube sample, did it freed the _data in some where else i did not notice? or it just not be freed? if _data not freed, may it cause memory inefficiency?