我有一个基于小级别的 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);
}
谁能想到完全删除我的声音文件的每一个痕迹(能够再次加载它)?
非常感谢你!