Crash in AVAudioSession currentRoute

Hi all,


We have received a number of crash reports from the app. They all look similar. The call [AVAudioSession currentRoute] is on the top of each crashed stack:


Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000c0b25beb8
Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [0]
Triggered by Thread:  14
...

Thread 14 name:
Thread 14 Crashed:
0  libobjc.A.dylib  objc_msgSend + 16
1  AVFAudio          -[AVAudioSession currentRoute] + 140 (AVAudioSession.mm:1715)
...


We found that the crash happens in the handler of AVAudioSessionRouteChangeNotification notification. In this handler we are posting an asyncronous call on our custom serial queue, where we are getting the current route and enumerating through all the outputs.


- (instancetype)init
{
    if (self = [super init])
    {
          _commandsQueue = dispatch_queue_create("com.app.commandsQueue", DISPATCH_QUEUE_SERIAL);
          [[NSNotificationCenter defaultCenter] addObserver:self
              selector:@selector(handleRouteChanged:)
              name:AVAudioSessionRouteChangeNotification
              object:nil];
    }
    return self;
}

- (void)handleRouteChanged:(NSNotification *)notification
{
    dispatch_async(self.commandsQueue, ^{
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        AVAudioSessionRouteDescription* currentRoute = audioSession.currentRoute;
        for (AVAudioSessionPortDescription *outputPort in currentRoute.outputs)
        {
            if ([outputPort.portType isEqualToString:AVAudioSessionPortAirPlay])
            {
                // updating dictionary with status
            }
        }
    });
}


We are not able to reproduce the issue and we do not know the exact scenario. We played a lot with different route change scenarios, but we observed no crashes.

Does anyone have a idea about the root cause of this crash?


Thanks,

Mike

Replies

Today I found one interesting thing. Not sure if it is related to the initial topic or not.

If I just call audioSession.currentRoute I'm getting a memory leak in AVAudioSessionPortDescription.


AVAudioSessionPortDescription 1 0x604000007aa0 16 Bytes AVFAudio -[AVAudioSessionRouteDescription initWithCategory:]
Malloc 96 Bytes 1 0x6040000a2dc0 96 Bytes AVFAudio -[AVAudioSessionPortDescription initWithPortType:]


My code snippet is simple:


- (void)viewDidLoad {
    [super viewDidLoad];
    [self testCurrentRoute];
}

- (void)testCurrentRoute {
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSLog(@"currentRoute %@", audioSession.currentRoute);
}


Can anyone from Apple help me to resolve this leak, and may be the crash from the initial post?


Thanks,

Mike

Hey Mike,


Did you find the cause of your crash by any chance? I am seeing a similar pattern for my App and it seems being produced while App is coming back alive from Background so I'm suspecting it's an inactive AVAudioSession (inactivated when going background) which is causing it (not being revived).


Cheers,


Arshia