【问题标题】:objc_msgSend crash Objective-Cobjc_msgSend 崩溃 Objective-C
【发布时间】:2019-10-24 22:55:03
【问题描述】:

我收到了来自实时用户的崩溃报告 我有 NSObject 有 nsstring

@interface MountedVolume : NSObject
@property (nonatomic, strong) NSString *name;
@end

视图控制器有文本字段,当通知时它的值会改变 收到了。

@interface ViewController : NSViewController {

      MountedVolume *selectedVolume;

    __weak IBOutlet NSTextField *txtVolumeName;

}
@end

@implementation ViewController

- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserverForName:DCNotificationNameVolumesUpdated object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        dispatch_async(dispatch_get_main_queue(), ^{

//crash report says in this line it got crashed

            txtVolumeName.stringValue = selectedVolume.name;

        });
    }];


}

@end

下面是崩溃报告,有什么建议可以解决这个问题吗?

崩溃报告

Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x7fff6ac2639d objc_msgSend + 29
1  AppKit                         0x7fff3db3d868 -[NSCell _objectValue:forString:errorDescription:] + 157
2  AppKit                         0x7fff3db3d71d -[NSCell setStringValue:] + 40
3  AppKit                         0x7fff3db95044 -[NSControl setStringValue:] + 135
4  MyApp               0x104de865b __40-[ViewController viewDidLoad]_block_invoke_3 (ViewController.m:172)
5  libdispatch.dylib              0x7fff6c3ab5f8 _dispatch_call_block_and_release + 12
6  libdispatch.dylib              0x7fff6c3ac63d _dispatch_client_callout + 8
7  libdispatch.dylib              0x7fff6c3b768d _dispatch_main_queue_callback_4CF + 1135
8  CoreFoundation                 0x7fff40435f56 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
9  CoreFoundation                 0x7fff40435683 __CFRunLoopRun + 2300
10 CoreFoundation                 0x7fff40434b35 CFRunLoopRunSpecific + 459
11 HIToolbox                      0x7fff3f71396b RunCurrentEventLoopInMode + 292
12 HIToolbox                      0x7fff3f7136a5 ReceiveNextEventCommon + 603
13 HIToolbox                      0x7fff3f713436 _BlockUntilNextEventMatchingListInModeWithFilter + 64
14 AppKit                         0x7fff3daad987 _DPSNextEvent + 965
15 AppKit                         0x7fff3daac71f -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1361
16 AppKit                         0x7fff3daa683c -[NSApplication run] + 699
17 AppKit                         0x7fff3da95d7c NSApplicationMain + 777
18 libdyld.dylib                  0x7fff6c3f93d5 start + 1

【问题讨论】:

  • 你确定selectedVolume.name的值在主循环中执行之前没有改变吗?

标签: objective-c macos crash appkit


【解决方案1】:

查看文档,stringValue 似乎不是可选的。您没有在 viewDidLoad 之前初始化 selectedVolume (据我所知),这意味着如果在为 selectedVolume 分配值之前运行,您可能会尝试在此通知块内将此值设置为 nil 。我首先尝试将 txtVolumeName.stringValue 设置为硬编码值,然后查看它是否完全停止崩溃。如果是这样,您可能需要在调用 setter 之前防范 nil 值。

例如

NSString *string;
NSTextField *textField = [[NSTextField alloc]init];
if (string) {
    textField.stringValue = string;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    相关资源
    最近更新 更多