释放非免费桥接对象的 NSArray

发布于 2024-12-11 12:08:28 字数 370 浏览 0 评论 0原文

我正在尝试管理我的应用程序中的钥匙串列表。我得到这样的钥匙串列表:

// _keychains is an instance variable in AppDelegate
_keychains = [NSArray array];
SecKeychainCopySearchList((CFArrayRef *)&_keychains);

所以基本上我得到 CFArrayRef 并将其转换为 NSArray。现在我的问题是如何释放钥匙串引用对象(不透明结构)?我是否必须将 NSArray 转换为 CFArrayRef 并使用 CFRelease (根据文档)或者是否可以以客观的方式释放内存?

I'm trying to manage a list of keychains in my application. I get the list of keychains like this:

// _keychains is an instance variable in AppDelegate
_keychains = [NSArray array];
SecKeychainCopySearchList((CFArrayRef *)&_keychains);

So basically I get the CFArrayRef and convert it to NSArray. Now my question is how do the keychain ref objects (opaque structs) get released? Do I have to convert NSArray to CFArrayRef and use CFRelease (as per documentation) or is it possible to free the memory in an objective way?

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

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

发布评论

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

评论(2

心房的律动 2024-12-18 12:08:28

您应该只需能够对数组调用 release 即可。 release 将释放数组并释放内容,就像 Cocoa 中一样。

在这种情况下,不存在如何释放 CF/NS-Array 的问题,并且在容器被释放之前您无需担心元素的生命周期。

即使通过 CFRelease 发送 SecKeychainRef 是非法的,您仍然应该期望它能成功,因为系统应该做正确的事情。 CoreFoundation 集合和分配器 API 对分配器和存储回调提供了足够的控制,即使在数组内容与默认行为不兼容的情况下,安全框架也应该执行正确的操作 (CFRelease< /code> 在这种情况下)。他们可以提供自己的回调和分配器。由于我没有看到任何文档表明与默认行为存在明显偏差,因此我认为在数组上调用 release 是安全的,并且这些元素要么与 CF 引用计数过程兼容,要么集合使用自定义分配器和/或回调方案(由安全性提供),并将根据需要清理元素(我的猜测是前者)。

You should simply be able to call release on the array. release will release the array and release the contents, just like in Cocoa.

In this case, there is no question how to release a CF/NS-Array, and you do not need to worry about the elements' lifetimes until the container has been released.

Even if were illegal to send SecKeychainRef through CFRelease, you should still expect this to work out because the system should do the right thing. CoreFoundation collection and allocator APIs offer enough control over allocator and store callbacks that the Security framework should do the right thing even in the event that the array's contents were not compatible with the default behavior (CFRelease in this case). They could provide their own callbacks and allocators. Since I see no documentation which states an obvious deviation from the default behaviour, I assume it is safe to call release on the array, and that the elements are either compatible with CF reference counting procedures, or that the collection uses a custom allocator and/or callback scheme (provided by Security) and will cleanup the elements as needed (my guess is the former).

指尖凝香 2024-12-18 12:08:28

使用完 _keychains 后,您只需执行 [_keychains release] 即可。当 _keychains 被释放时,_keychains 对象将向其每个元素发送一条 release 消息。

You can just do [_keychains release] when you're done with _keychains. The _keychains object will send a release message to each of its elements when _keychains is deallocated.

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