重新加载先前卸载的捆绑包时 KVO/KVC 崩溃

发布于 2025-01-05 03:35:36 字数 1894 浏览 1 评论 0 原文

我遇到了可加载包和 KVO 的问题。似乎任何具有 KVO 观察者附加到其实例的类都无法通过 NSBundle 的 -unload 方法安全地卸载。

我正在执行以下操作:

for (int i = 0; i < 100; i++)
{
    [bundle load];

    Class bundleClass = [bundle principalClass];

    [[[bundleClass alloc] init] release];

    [bundle unload];
}

在捆绑包的原理类 -init 方法中,

    [self addObserver: self 
           forKeyPath: @"name"
              options: 0
              context: nil];

    self.name = @"jim";

循环经过多次迭代,有时在第二轮崩溃,有时在第三十轮崩溃。

它总是因这个回溯和 EXC_BAD_ACCESS 信号而崩溃。

#0  0x00007fff8a30deab in objc_msgSend ()
#1  0x00007fff8609d862 in NSKeyValueNotifyObserver ()
#2  0x00007fff860be99b in NSKeyValueDidChange ()
#3  0x00007fff8606b0fb in -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] ()
#4  0x00000001007a4c2c in -[Bundle init] (self=0x101902130, _cmd=0x7fff8ea369b0) at /Users/joerick/Desktop/bundleTest/testbundle/Bundle.m:26
#5  0x0000000100001731 in -[SIAppDelegate applicationDidFinishLaunching:] (self=0x100326a90, _cmd=0x7fff876e285f, aNotification=0x100131ea0) at /Users/joerick/Desktop/bundleTest/bundleTest/SIAppDelegate.m:28
#6  0x00007fff8606ade2 in __-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_1 ()
#7  0x00007fff8b470e0a in _CFXNotificationPost ()
#8  0x00007fff86057097 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#9  0x00007fff8e1bbaa7 in -[NSApplication _postDidFinishNotification] ()
#10 0x00007fff8e1bb80d in -[NSApplication _sendFinishLaunchingNotification] ()
....

此处有完整代码

您可以下载显示此问题的示例项目这里

我认为这是 Cocoa 中的一个错误,但我想知道是否有人能看到我在这里做了什么愚蠢的事情?

I'm running into an issue with loadable bundles and KVO. It seems that any class that has KVO observers attached to an instance of it cannot be safely be unloaded via NSBundle's -unload method.

I'm doing the following:

for (int i = 0; i < 100; i++)
{
    [bundle load];

    Class bundleClass = [bundle principalClass];

    [[[bundleClass alloc] init] release];

    [bundle unload];
}

And in the bundle's principle class -init method,

    [self addObserver: self 
           forKeyPath: @"name"
              options: 0
              context: nil];

    self.name = @"jim";

The loop gets through a number of iterations, sometimes crashing on the second time round, sometimes on the thirtieth.

It always crashes with this backtrace, with the EXC_BAD_ACCESS signal.

#0  0x00007fff8a30deab in objc_msgSend ()
#1  0x00007fff8609d862 in NSKeyValueNotifyObserver ()
#2  0x00007fff860be99b in NSKeyValueDidChange ()
#3  0x00007fff8606b0fb in -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] ()
#4  0x00000001007a4c2c in -[Bundle init] (self=0x101902130, _cmd=0x7fff8ea369b0) at /Users/joerick/Desktop/bundleTest/testbundle/Bundle.m:26
#5  0x0000000100001731 in -[SIAppDelegate applicationDidFinishLaunching:] (self=0x100326a90, _cmd=0x7fff876e285f, aNotification=0x100131ea0) at /Users/joerick/Desktop/bundleTest/bundleTest/SIAppDelegate.m:28
#6  0x00007fff8606ade2 in __-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_1 ()
#7  0x00007fff8b470e0a in _CFXNotificationPost ()
#8  0x00007fff86057097 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#9  0x00007fff8e1bbaa7 in -[NSApplication _postDidFinishNotification] ()
#10 0x00007fff8e1bb80d in -[NSApplication _sendFinishLaunchingNotification] ()
....

Full code here

You can download a sample project showing this issue here.

I'm thinking that this is a bug in Cocoa, but I wonder if anybody could see if I'm doing anything stupid here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2025-01-12 03:35:36

我已向 Apple 报告了此事。

rdar://11017946

I've reported this to Apple.

rdar://11017946

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文