Skip to content

Commit

Permalink
Fixed a few retain cycle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltyson committed Jun 14, 2016
1 parent 8cfec37 commit 738d905
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
28 changes: 13 additions & 15 deletions TheAmazingAudioEngine/Modules/Generation/AEAudioFilePlayerModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -228,26 +228,24 @@ BOOL AEAudioFilePlayerModuleGetPlaying(__unsafe_unretained AEAudioFilePlayerModu
}

- (void)setupMainThreadEndpoint {
__weak AEAudioFilePlayerModule * weakSelf = self;
__unsafe_unretained AEAudioFilePlayerModule * weakSelf = self;
self.mainThreadEndpointValue.objectValue
= [[AEMainThreadEndpoint alloc] initWithHandler:^(const void * data, size_t length) {
AEAudioFilePlayerModule * self = weakSelf;

if ( self.beginBlock && self->_anchorTime != 0 ) {
self.beginBlock();
self.beginBlock = nil;
if ( !self.completionBlock || self.loop ) {
self.mainThreadEndpointValue.objectValue = nil;
if ( weakSelf.beginBlock && weakSelf->_anchorTime != 0 ) {
weakSelf.beginBlock();
weakSelf.beginBlock = nil;
if ( !weakSelf.completionBlock || weakSelf.loop ) {
weakSelf.mainThreadEndpointValue.objectValue = nil;
}
}

if ( self->_stopEventScheduled ) {
self->_stopEventScheduled = NO;
AECheckOSStatus(AudioUnitReset(self.audioUnit, kAudioUnitScope_Global, 0), "AudioUnitReset");
self->_sequenceScheduled = NO;
self->_playing = NO;
if ( self.completionBlock ) self.completionBlock();
self.mainThreadEndpointValue.objectValue = nil;
if ( weakSelf->_stopEventScheduled ) {
weakSelf->_stopEventScheduled = NO;
AECheckOSStatus(AudioUnitReset(weakSelf.audioUnit, kAudioUnitScope_Global, 0), "AudioUnitReset");
weakSelf->_sequenceScheduled = NO;
weakSelf->_playing = NO;
if ( weakSelf.completionBlock ) weakSelf.completionBlock();
weakSelf.mainThreadEndpointValue.objectValue = nil;
}
}];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#import <AVFoundation/AVFoundation.h>

@interface AEAudioUnitInputModule ()
@property (nonatomic, strong) AEIOAudioUnit * ioUnit;
@property (nonatomic, weak) AEIOAudioUnit * ioUnit;
@property (nonatomic, readwrite) int numberOfInputChannels;
@property (nonatomic, strong) id ioUnitStreamChangeObserverToken;
@property (nonatomic) BOOL ownsIOUnit;
Expand All @@ -55,7 +55,8 @@ - (instancetype)initWithRenderer:(AERenderer *)renderer audioUnit:(AEIOAudioUnit
if ( audioUnit ) {
self.ioUnit = audioUnit;
} else {
self.ioUnit = [AEIOAudioUnit new];
// We need to add a bridging retain, because the property is weak
self.ioUnit = (__bridge AEIOAudioUnit*) CFBridgingRetain([AEIOAudioUnit new]);
self.ioUnit.inputEnabled = YES;
self.ioUnit.sampleRate = self.renderer.sampleRate;
self.ownsIOUnit = YES;
Expand All @@ -81,6 +82,10 @@ - (instancetype)initWithRenderer:(AERenderer *)renderer audioUnit:(AEIOAudioUnit
}

- (void)dealloc {
if ( self.ownsIOUnit ) {
// Add a bridging release, because we manually retained the audio unit
CFBridgingRelease((__bridge CFTypeRef)self.ioUnit);
}
[[NSNotificationCenter defaultCenter] removeObserver:self.ioUnitStreamChangeObserverToken];
}

Expand Down
3 changes: 2 additions & 1 deletion TheAmazingAudioEngine/Modules/Generation/AEMixerModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ @implementation AEMixerModule
- (instancetype)initWithRenderer:(AERenderer *)renderer {
if ( !(self = [super initWithRenderer:renderer]) ) return nil;

__weak AEMixerModule * weakSelf = self;
self.array = [[AEArray alloc] initWithCustomMapping:^void * _Nonnull(id _Nonnull item) {
return [self newEntryForModule:item volume:1.0 balance:0.0];
return [weakSelf newEntryForModule:item volume:1.0 balance:0.0];
}];
[self.array updateWithContentsOfArray:@[]];

Expand Down
4 changes: 2 additions & 2 deletions TheAmazingAudioEngine/Outputs/AEAudioUnitOutput.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ - (instancetype)initWithRenderer:(AERenderer *)renderer {

if ( rateChanged ) {
[[NSNotificationCenter defaultCenter]
postNotificationName:AEAudioUnitOutputDidChangeSampleRateNotification object:self];
postNotificationName:AEAudioUnitOutputDidChangeSampleRateNotification object:weakSelf];
}

if ( channelsChanged ) {
[[NSNotificationCenter defaultCenter]
postNotificationName:AEAudioUnitOutputDidChangeNumberOfOutputChannelsNotification object:self];
postNotificationName:AEAudioUnitOutputDidChangeNumberOfOutputChannelsNotification object:weakSelf];
}
}];

Expand Down

0 comments on commit 738d905

Please sign in to comment.