Skip to content

Commit

Permalink
Fix release()-ing of items from the callback pool (#476)
Browse files Browse the repository at this point in the history
* Fix release()-ing of items from the callback pool

These were being removed according to the wrong key.

* Fix race condition looking up key

* Add more synchronization
  • Loading branch information
slycoder authored and zmxv committed Feb 3, 2019
1 parent f3b0183 commit 146a6e2
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions RNSound/RNSound.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ -(NSString *) getDirectory:(int)directory {

-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer*)player
successfully:(BOOL)flag {
NSNumber* key = [self keyForPlayer:player];
if (key == nil) return;
@synchronized(self) {
NSNumber* key = [self keyForPlayer:player];
if (key == nil) return;

@synchronized(key) {
[self setOnPlay:NO forPlayerKey:key];
RCTResponseSenderBlock callback = [self callbackForKey:key];
if (callback) {
Expand Down Expand Up @@ -199,12 +199,14 @@ -(NSDictionary *)constantsToExport {
}

if (player) {
player.delegate = self;
player.enableRate = YES;
[player prepareToPlay];
[[self playerPool] setObject:player forKey:key];
callback(@[[NSNull null], @{@"duration": @(player.duration),
@"numberOfChannels": @(player.numberOfChannels)}]);
@synchronized(self) {
player.delegate = self;
player.enableRate = YES;
[player prepareToPlay];
[[self playerPool] setObject:player forKey:key];
callback(@[[NSNull null], @{@"duration": @(player.duration),
@"numberOfChannels": @(player.numberOfChannels)}]);
}
} else {
callback(@[RCTJSErrorFromNSError(error)]);
}
Expand Down Expand Up @@ -240,13 +242,15 @@ -(NSDictionary *)constantsToExport {
}

RCT_EXPORT_METHOD(release:(nonnull NSNumber*)key) {
AVAudioPlayer* player = [self playerForKey:key];
if (player) {
[player stop];
[[self callbackPool] removeObjectForKey:player];
[[self playerPool] removeObjectForKey:key];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
@synchronized(self) {
AVAudioPlayer* player = [self playerForKey:key];
if (player) {
[player stop];
[[self callbackPool] removeObjectForKey:key];
[[self playerPool] removeObjectForKey:key];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
}
}
}

Expand Down

0 comments on commit 146a6e2

Please sign in to comment.