2015-08-15 111 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零,並且解決它