2015-08-15 95 views
1

我创建了AVAudioUnitSampler,但在通过Instruments工具检查内存后,看起来在场景改变时,即使AVAudioUnitSampler对象为零,它第一次初始加载的资源仍然是在记忆中。当我重新创建采样器时,它会重新加载资源,现在我已经为采样器增加了两倍的内存。我如何强制资源释放?AVAudioUnitSampler未释放资源

下面的代码:

-(void) loadSampler { 
    // Instatiate audio engine 
    _engine = [[AVAudioEngine alloc] init]; 
    _mixer = [_engine mainMixerNode]; 
    _sampler = [[AVAudioUnitSampler alloc] init]; 

    [self loadSoundFontInstrument]; //sound font is the instrument that the sampler will use to play sounds 

    [self makeEngineConnections]; 
    [self startEngine]; 
} 

-(void) loadSoundFontInstrument { 
    if (_sampler != nil) { 
     NSString* instrument = [[GameData sharedGameData].settings valueForKey:@"instrument"]; //decides on what sound font file to use 
     NSURL *piano = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:instrument ofType:@"sf2"]]; 
     [_sampler loadSoundBankInstrumentAtURL:piano program:0 bankMSB:0x79 bankLSB:0 error:nil]; 
    } 
    else 
     NSLog(@"ERROR: Sampler has not been initialized"); 
} 

-(void)makeEngineConnections { 
    [_engine attachNode:_sampler]; 
    [_engine connect:_sampler to:_mixer format:[_sampler outputFormatForBus:0]]; 
} 

-(void)startEngine { 
    [_engine startAndReturnError:nil]; 
} 

回答

2

我解决了这个问题,但我不完全知道为什么我的解决方案解决了这个问题。当看到Leaks仪器时,我注意到_sampler变量的保留计数为2,这个类(你看不到的类叫MIDIController拥有_sampler对象,_engine对象也保留了对采样器的引用,即使当我让我MIDI控制器对象为零,采样器仍保留在内存中,保留计数为1.奇怪的是,由于它的父代已被释放,因此我不确定它为什么仍然存在,因此不再提及对象_engine

TLDR:总之,我做了_sampler_engine_mixer零,并且解决它