如何完全删除和释放OpenAL声音文件的内存?

时间:2010-01-10 03:28:47

标签: iphone audio release openal

我有一个基于小级别的iPhone应用程序。我需要为每个级别加载和释放声音文件。 除了释放声音之外,我的openAL SoundManager一切正常。

首先,当我移除声音时,它似乎正在做它应该做的事情 - 它会移除声音,除非我重新加载它,否则我无法再次访问它。但是,当我用'仪器'测试我的应用程序dealloc时,它没有显示任何释放。它似乎没有释放内存。因此,当您从一个级别移动到另一个级别时,内存耗尽并且应用程序崩溃不会花费很长时间。

我在控制台中收到此错误:

  

编程接收信号:“0”。   警告:check_safe_call:无法恢复当前帧   杀   退出

这是我加载声音的方式 -

- (void)loadSoundWithKey:(NSString*)aSoundKey fileName:(NSString*)aFileName fileExt:(NSString*)aFileExt {
// Check to make sure that a sound with the same key does not already exist
NSNumber *numVal = [soundLibrary objectForKey:aSoundKey];
// If the key is found log it and finish
if(numVal != nil) {
    NSLog(@"WARNING - SoundManager: Sound key '%@' already exists.", aSoundKey);
    return;
}
    NSUInteger bufferID;
// Generate a buffer within OpenAL for this sound
alGenBuffers(1, &bufferID);
// Set up the variables which are going to be used to hold the format
// size and frequency of the sound file we are loading
ALenum  error = AL_NO_ERROR;
ALenum  format;
ALsizei size;
ALsizei freq;
ALvoid *data;
NSBundle *bundle = [NSBundle mainBundle];
// Get the audio data from the file which has been passed in
CFURLRef fileURL = (CFURLRef)[[NSURL fileURLWithPath:[bundle pathForResource:aFileName ofType:aFileExt]] retain];
if (fileURL)
{   
    data = MyGetOpenALAudioData(fileURL, &size, &format, &freq);
    CFRelease(fileURL);
    if((error = alGetError()) != AL_NO_ERROR) {
        NSLog(@"ERROR - SoundManager: Error loading sound: %x\n", error);
        exit(1);
    }
    // Use the static buffer data API
    alBufferDataStaticProc(bufferID, format, data, size, freq);
    if((error = alGetError()) != AL_NO_ERROR) {
        NSLog(@"ERROR - SoundManager: Error attaching audio to buffer: %x\n", error);
    }       
}
else
{
    NSLog(@"ERROR - SoundManager: Could not find file '%@.%@'", aFileName, aFileExt);
    data = NULL;
}

// Place the buffer ID into the sound library against |aSoundKey|
[soundLibrary setObject:[NSNumber numberWithUnsignedInt:bufferID] forKey:aSoundKey];
if(DEBUG) NSLog(@"INFO - SoundManager: Loaded sound with key '%@' into buffer '%d'", aSoundKey, bufferID);

}

这就是我试图删除/释放的方式。但它似乎仍然保留了声音文件的记忆 -

- (void)removeSoundWithKey:(NSString*)aSoundKey {
// Find the buffer which has been linked to the sound key provided
NSNumber *numVal = [soundLibrary objectForKey:aSoundKey];
// If the key is not found log it and finish
if(numVal == nil) {
    NSLog(@"WARNING - SoundManager: No sound with key '%@' was found so cannot be removed", aSoundKey);
    return;
}    
// Get the buffer number form the sound library so that the sound buffer can be released
NSUInteger bufferID = [numVal unsignedIntValue];
alDeleteBuffers(1, &bufferID);
[soundLibrary removeObjectForKey:aSoundKey];
if(DEBUG) NSLog(@"INFO - SoundManager: Removed sound with key '%@'", aSoundKey);

}

有人可以想到完全删除我的声音文件的每一个痕迹(能够再次加载它)吗?

非常感谢!

3 个答案:

答案 0 :(得分:5)

如果有人有兴趣...我的问题通过将alBufferDataStaticProc更改为alBufferData来解决。然后添加if(data) free(data);见下文。

感谢您的帮助。

alBufferData(bufferID, format, data, size, freq);
    if((error = alGetError()) != AL_NO_ERROR) {
        NSLog(@"ERROR - SoundManager: Error attaching audio to buffer: %x\n", error);
    }
    if (data) 
        free(data);
}else{
    NSLog(@"ERROR - SoundManager: Could not find file '%@.%@'", fileName, fileType);
    data = NULL;

答案 1 :(得分:2)

这是我做“”以完全删除我的声音文件的每一个痕迹(能够再次加载它)的总结“使用相同的apple example和一些{ {3}}:

if (device != NULL)
    [self teardownOpenAL];
[self initOpenAL];
alSourcePlay(source);

此处的主要补充是if,在我的情况下,我实际上已添加到teardownOpenAL

答案 2 :(得分:0)

不熟悉OpenAL for iPhone的专业,但这是我的粗略猜测:

缓冲区是否仍然附加到OpenAL源,即您是否在加载和发布之间调用了alSourcei(...,AL_BUFFER,...)或alSourceQueueBuffers?如果是这种情况,您可能需要在删除缓冲区之前运行alSourcei(...,AL_BUFFER,NULL)或alSourceUnqueueBuffers。

在调用alDeleteBuffers后尝试检查OpenAL错误(alGetErrors)。