释放非免费桥接对象的 NSArray
我正在尝试管理我的应用程序中的钥匙串列表。我得到这样的钥匙串列表:
// _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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该只需能够对数组调用
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
throughCFRelease
, 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 callrelease
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).使用完
_keychains
后,您只需执行[_keychains release]
即可。当_keychains
被释放时,_keychains
对象将向其每个元素发送一条release
消息。You can just do
[_keychains release]
when you're done with_keychains
. The_keychains
object will send arelease
message to each of its elements when_keychains
is deallocated.