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

标签 iphone audio release openal

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

起初,当我删除声音时,它似乎做了它应该做的事情 - 它删除了声音,除非重新加载,否则我无法再次访问它。但是,当我使用“Instruments”测试我的应用程序的释放时,它没有显示任何释放。看来并没有释放内存。因此,当您从一个级别移动到另一个级别时,内存很快就会耗尽并且应用程序崩溃。

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

Program received signal: “0”. warning: check_safe_call: could not restore current frame kill quit

这就是我加载声音的方式 -

- (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);

}

有人能想出办法完全删除我的声音文件的所有痕迹(并且能够再次加载它)吗?

非常感谢!

最佳答案

如果有人感兴趣...我的问题已通过将 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;

关于iphone - 如何完全删除并释放 OpenAL 声音文件的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2035975/

相关文章:

iphone - 更改已设置的 UILocalNotification 的 soundName

C 程序在 Debug模式下工作,在 Release模式下不工作

java - Maven 构建和发布多个相互依赖的项目

ios - Flutter iOS - 在没有真实设备的情况下构建发布版本?

iphone - 如何在枚举 UIScrollView 的 subview 时排除滚动指示器?

iphone - 播放 iPod 库中的多首歌曲

通过激光进行 Python 音频传输

javascript - mediaelement.js音频播放器需要修复以适应移动响应大小

ios - 打印 Alamofire 请求正文

objective-c - 使用Cocoa录制音频并将音频发送到Xcode中的API