gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:39:29 34 4
gpt4 key购买 nike

我有一个基于小型关卡的 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com