0

Whilst, I have been unable to replicate this issue on my test devices / simulator, I'm getting a few crash reports from some users, but not all. EXC_BAD_ACCESS KERN_INVALID_ADDRESS at this line. Why is this and what steps can I take to resolve? The crash reports are from users on iOS 14.4.0 and 14.3.0 and from a variety of devices (iPhone 6s plus, iPhone 8 Plus, iPhone SE (2nd generation), iPhone XS, iPhone 7, iPhone 7s, iPhone 11, iPhone 12, iPhone X)

In PageViewController.m

// Voice Recording - Needed as workaround as there is a bug in AudioKit
NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                           defaultVoiceRecording,
                           nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

// Setup audio session to play through loud speakers
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
if(error){
    NSLog(@"AVAudioSession Sing the Note error tuner:%@",error);
}

// Setup audio session to play through loud speakers
if (![MenuViewController areHeadphonesPluggedIn]){ // EXC_BAD_ACCESS KERN_INVALID_ADDRESS here
    
    [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
    if(error)
    {
        NSLog(@"Error: AudioSession cannot use speakers");
    }
}

In MenuViewController.h

+(BOOL)areHeadphonesPluggedIn;

In MenuViewController.m

+(BOOL)areHeadphonesPluggedIn {
    @try{
        NSArray *availableOutputs = [[AVAudioSession sharedInstance] currentRoute].outputs;
        for (AVAudioSessionPortDescription *portDescription in availableOutputs) {
            if ([portDescription.portType isEqualToString:AVAudioSessionPortHeadphones]) {
                return YES;
            }
        }
        return NO;
    }
    @catch(id Exception){
        return NO;
    }
}
TomV
  • 1,157
  • 1
  • 13
  • 25
  • Does this help https://stackoverflow.com/questions/25683275/ios-crash-exc-bad-access-kern-invalid-address - maybe the trouble is in PageViewController somewhere. Can you show a bit more code around the first snippet you provide. – skaak Mar 17 '21 at 06:50
  • 1
    After reading that page, am I correct in summarising that it's due to a 'dangling' pointer somewhere up the stack, and not where the error log crash reports say it is? – TomV Mar 17 '21 at 09:48
  • I *think* so - the code you give looks pretty standard, so I think PageViewController or one of its members are 'dangling' ... – skaak Mar 17 '21 at 09:51

2 Answers2

0
+(BOOL)areHeadphonesPluggedIn {
    @try{
        NSArray *availableOutputs = [[AVAudioSession sharedInstance] currentRoute].outputs;
        for (AVAudioSessionPortDescription *portDescription in availableOutputs) {
            if ([portDescription.portType isEqualToString:AVAudioSessionPortHeadphones]) {
                return YES;
            }
        }
        return NO;
    }
    @catch(id Exception){
        return NO;  // Not sure it will work as you exect
    }
   return NO;  // move return here!
}
0

This issue was down to a 'dangling' pointer somewhere up the stack, in this case it was the autorelease array pathComponents. Needed to change that to non-autorelease array.

The full solution PageViewController.m

   // Change from autorelease to normal non-autorelease
   NSArray *pathComponentsSN = [[NSArray alloc] initWithObjects:     
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],defaultVoiceRecording,nil];
    
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponentsSN];
    
    // Setup audio session to play through loud speakers
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *error;
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    if(error){
        NSLog(@"AVAudioSession Sing the Note error creating session");
    }
    
    // Setup audio session to play through loud speakers
    if (![MenuViewController areHeadphonesPluggedIn]){ // Crash logs from this line
        
        [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
        if(error)
        {
            NSLog(@"Error: AudioSession cannot use speakers");
        }
    }
TomV
  • 1,157
  • 1
  • 13
  • 25