ios - Objective-C中的EXC_BAD_ACCESS错误

标签 ios objective-c exc-bad-access

我正在尝试编写一个简单的iOS程序,该程序将在插入/拔出耳机时发出通知。我是Objective-CiOS的新手,很多代码是由其他人编写的,我现在正尝试对其进行调整,因此在解决此问题时遇到了问题。代码在这里:

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

static void onHeadsetChange(void *, AudioServicesPropertyID, UInt32, const void *);

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.headsetOnHandler = self.headsetOffHandler = ^{};
    NSLog(@"%lu", AudioSessionInitialize(NULL, NULL, NULL, NULL));
    NSLog(@"%lu", AudioSessionSetActive(true));
    NSLog(@"%lu", AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, &onHeadsetChange, (__bridge void *)self));
    return YES;
}

static void onHeadsetChange(void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue) {
    if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;
    NSLog(@"The headset changed");
    AppDelegate *const delegate = (__bridge AppDelegate *)inUserData;

    const CFDictionaryRef routeChangeDictionary = inPropertyValue;
    const CFNumberRef routeChangeReasonRef = CFDictionaryGetValue(routeChangeDictionary, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
    SInt32 routeChangeReason;
    CFNumberGetValue(routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
    switch (routeChangeReason) {
        case kAudioSessionRouteChangeReason_NewDeviceAvailable:
            //NSLog([delegate description]);
            [delegate headsetOnHandler];
            break;
        case kAudioSessionRouteChangeReason_OldDeviceUnavailable:
            [delegate headsetOffHandler];
            break;
        default:
            break;
    }
}
@end

ViewController.m
#import "ViewController.h"

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    AppDelegate *delegate = (AppDelegate *)([UIApplication sharedApplication].delegate);
    delegate.headsetOnHandler = ^{ self.view.backgroundColor = [UIColor greenColor];};
    delegate.headsetOffHandler = ^{ self.view.backgroundColor = [UIColor redColor];};
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我正在获取EXC_BAD_ACCESS error, code=1 at [delegate headsetOnHandler] and [delegate headsetOffHandler]。如果需要为此添加.h文件,我会的,如果还有其他遗漏的地方,请告诉我。

编辑:
我添加了人们认为应该添加的声明:
@property (readwrite, assign) void (^headsetOnHandler)(void);
@property (readwrite, assign) void (^headsetOffHandler)(void);

最佳答案

如何声明headsetOnHandlerheadsetOffHandler属性?

如果是assignretain,则可能是问题的根源。由于它们是块,因此它必须是copy(即使在某些情况下,在ARC下)。如果它们是atomicretain,那么您的getter将有效地在getter中调用[[block retain] autorelease],这将解释您的崩溃。

有点远射。

  • 发生当机时,请务必回溯
  • 当您知道崩溃所在的代码行时,请始终在该行上张贴任何变量的变量声明

  • 请注意,ARC会稍微改变这种行为,在某些情况下会自动处理这些块,但是-IIRC-在这种情况下,您仍然需要对其进行copy编码。

    关于ios - Objective-C中的EXC_BAD_ACCESS错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16683994/

    相关文章:

    ios - 来自 block 的 @autoreleasepool 中的 EXC_BAD_ACCESS

    iOS——UITextView 禁用滚动但启用触摸

    iphone - 应用播放背景音频不起作用

    iphone - 我应该重新发送针对iOS5优化的应用程序吗? (上周日/iOS4发送)

    ios - tableview 和按钮的奇怪行为

    objective-c - block 作为 C 函数的参数或返回值

    ios - 设置 NSObject 属性 - EXC_BAD_ACCESS

    objective-c - EXC_BAD_ACCESS,但不是在使用断点时

    ios - 如何将单个 tileset 图像导入 xCode(Sprite Kit)?

    ios - 如何在一个自定义 tableView 单元格中使用两个按钮?我想在单击一个按钮时更改它们的图标