【问题标题】:How to fix the crash in CFNotificationCenterAddObserver for NSWorkspaceDidActivateApplicationNotification?如何修复 NSWorkspaceDidActivateApplicationNotification 的 CFNotificationCenterAddObserver 崩溃?
【发布时间】:2022-01-10 00:18:53
【问题描述】:

不太习惯Objective C,因此尝试用CFNotificationCenterAddObserver()方法观察C++代码中的NSWorkspaceDidActivateApplicationNotification事件。

#include<CoreFoundation/CFNotificationCenter.h>
#include<iostream>
#include<QGuiApplication>

void Callback (CFNotificationCenterRef center, void* observer, CFNotificationName name,
               const void* object,
               CFDictionaryRef userInfo)
{
  std::cout << "Hello World\n";
  //NSLog(@"New application: %@", [[name userInfo] objectForKey:NSWorkspaceApplicationKey]);
}

int main (int argc, char** argv)
{
  QGuiApplication application(argc, argv);

  std::cout << "Add observer\n";
  CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(), nullptr, Callback,
                                  CFStringRef("NSWorkspaceDidActivateApplicationNotification"), nullptr,
                                  CFNotificationSuspensionBehaviorDeliverImmediately);
  std::cout << "Added ...\n";
  return application.exec();
}

但是它会导致以下崩溃。如果我尝试在调试模式下运行,它有时会显示以下崩溃或有时会卡在该函数本身。

Add observer
2021-12-03 21:44:24.527086+0530 Test[17288:982994] -[__NSTaggedDate length]: unrecognized selector sent to instance 0x100007c65
2021-12-03 21:44:24.527916+0530 Test[17288:982994] [General] An uncaught exception was raised
2021-12-03 21:44:24.527943+0530 Test[17288:982994] [General] -[__NSTaggedDate length]: unrecognized selector sent to instance 0x100007c65
2021-12-03 21:44:24.528044+0530 Test[17288:982994] [General] (
    0   CoreFoundation                      0x00007fff35570035 __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff5fc80a17 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff355e9fce -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff35511e9f ___forwarding___ + 1485
    4   CoreFoundation                      0x00007fff35511848 _CF_forwarding_prep_0 + 120
    5   CoreFoundation                      0x00007fff355aca4c __CFXNotificationRegisterObserver + 588
    6   CoreFoundation                      0x00007fff3548a037 CFNotificationCenterAddObserver + 204
    7   Test                                0x00000001000066a1 main + 129
    8   libdyld.dylib                       0x00007fff614503d5 start + 1
)
2021-12-03 21:44:24.528241+0530 Test[17288:982994] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSTaggedDate length]: unrecognized selector sent to instance 0x100007c65'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff35570035 __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff5fc80a17 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff355e9fce -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff35511e9f ___forwarding___ + 1485
    4   CoreFoundation                      0x00007fff35511848 _CF_forwarding_prep_0 + 120
    5   CoreFoundation                      0x00007fff355aca4c __CFXNotificationRegisterObserver + 588
    6   CoreFoundation                      0x00007fff3548a037 CFNotificationCenterAddObserver + 204
    7   Test                                0x00000001000066a1 main + 129
    8   libdyld.dylib                       0x00007fff614503d5 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

如何解决这个问题?

【问题讨论】:

  • 该错误意味着在某些时候您认为您正在使用NSString 对象,并在其上调用length(内部调用它,隐藏Apple 的SDK 代码)方法。但实际上它是一个NSDate(或内部方法说NSTaggedDate)对象,它不知道length 方法并崩溃。
  • @Larme,那么应该做些什么来解决这个错误。介意发布答案吗?
  • NSWorkspace 有自己的notificationCenter

标签: c++ objective-c qt nsnotificationcenter core-foundation


【解决方案1】:

使用CFSTR 宏从常量 C 字符串创建 CFStringRef,如下所示:

CFSTR("NSWorkspaceDidActivateApplicationNotification")

NSWorkspaceDidActivateApplicationNotification docs 表示必须使用NSWorkspace.notificationCenter 接收,CFNotificationCenterGetDistributedCenter 不起作用。

【讨论】:

  • 这只修复了崩溃。但是,仍然没有调用 Callback()。还有什么遗漏的吗?
  • 更新了答案。我建议将您的文件重命名为 .mm,并使用 Objective-C AppKit API 而不是 CoreFoundation C API。可以将 C++ 和 Objective-C 组合在一个 .mm 文件中。
猜你喜欢
  • 2019-10-07
  • 2018-01-18
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多