- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 SecItemCopyMatching 上发现内存泄漏。经过对 SF 的调查,我找到了解决方案:
__block NSString *certificateName = nil;
SecKeychainRef keychain;
SecKeychainCopyDefault(&keychain);
NSMutableDictionary *attributeQuery = [NSMutableDictionary dictionary];
[attributeQuery setObject: (id) kSecClassIdentity forKey:(__bridge_transfer id) kSecClass];
[attributeQuery setObject: (id) kCFBooleanTrue forKey:(__bridge_transfer id) kSecReturnRef];
[attributeQuery setObject: (id) kSecMatchLimitAll forKey:(__bridge_transfer id) kSecMatchLimit];
CFTypeRef attrResult = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef) attributeQuery,(CFTypeRef *) &attrResult);<------- here is a leak according Instruments
if (status != errSecItemNotFound) {
NSArray *attributeResult = (__bridge_transfer NSArray *)attrResult;
[attributeResult enumerateObjectsUsingBlock:^(id identityFromArray, NSUInteger idx, BOOL *stop) {
OSStatus status;
SecCertificateRef cert = NULL;
status = SecIdentityCopyCertificate((__bridge SecIdentityRef)identityFromArray, &cert);
if (!status)
{
或其他解决方案:
NSMutableDictionary *attributeQuery = [NSMutableDictionary dictionary];
[attributeQuery setObject: (id) kSecClassIdentity forKey:(__bridge_transfer id) kSecClass];
[attributeQuery setObject: (id) kCFBooleanTrue forKey:(__bridge_transfer id) kSecReturnRef];
[attributeQuery setObject: (id) kSecMatchLimitAll forKey:(__bridge_transfer id) kSecMatchLimit];
CFTypeRef attrResult = NULL;
CFDictionaryRef cfquery = (__bridge_retained CFDictionaryRef)attributeQuery;
OSStatus status = SecItemCopyMatching(cfquery,(CFTypeRef *) &attrResult);<------- here is a leak according Instruments
if (status != errSecItemNotFound) {
NSArray *attributeResult = (__bridge_transfer NSArray *)attrResult;
[attributeResult enumerateObjectsUsingBlock:^(id identityFromArray, NSUInteger idx, BOOL *stop) {
OSStatus status;
SecCertificateRef cert = NULL;
status = SecIdentityCopyCertificate((__bridge SecIdentityRef)identityFromArray, &cert);
if (!status)
{
char *nameBuf = NULL;
CFStringRef nameRef = NULL;
OSStatus statusNew = SecCertificateInferLabel(cert, &nameRef);
.....
CFRelease(cfquery)
但他们仍然对我来说泄漏。
还有什么想法
最佳答案
Copy
的 CF 样式函数接收钥匙串(keychain)对象。因此它有一个 +1 引用计数,并且您有责任在使用完它后显式释放它。您的示例代码从未发布它,因此它正在泄漏。您发布的代码中从未使用过钥匙串(keychain)对象,因此可以完全消除它。__bridge
转换传递 attributeQuery
(局部变量),这不是一个好主意; ARC 可能会过早地从你的脚下释放它。您应该使用 __bridge_retained
(或 CFBridgingRetain
)将其转换为保留计数 +1 的 CF 国家/地区(并在稍后明确释放它)。__bridge_retained
,但没有发布结果,这解释了泄漏的原因。SecItemCopyMatching
的返回值为零。您不应仅与 errSecItemNotFound
进行比较;查询失败可能有多种其他原因。更新的代码:
NSMutableDictionary *attributeQuery = [NSMutableDictionary dictionary];
[attributeQuery setObject:(id)kSecClassIdentity forKey:(__bridge id)kSecClass];
[attributeQuery setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnRef];
[attributeQuery setObject:(id)kSecMatchLimitAll forKey:(__bridge id)kSecMatchLimit];
CFTypeRef cfresult = NULL;
CFDictionaryRef cfquery = (CFDictionaryRef)CFBridgingRetain(attributeQuery);
OSStatus status = SecItemCopyMatching(cfquery, &cfresult);
CFRelease(cfquery);
if (status == errSecSuccess) {
NSArray *attributeResult = CFBridgingRelease(cfresult);
[attributeResult enumerateObjectsUsingBlock:^(id value, NSUInteger idx, BOOL *stop) {
OSStatus status;
SecCertificateRef cert = NULL;
SecIdentityRef identity = CFBridgingRetain(value);
status = SecIdentityCopyCertificate(identity, &cert);
CFRelease(identity);
if (!status)
{
...
CFRelease(cert);
}];
}
我发现 Core Foundation/Cocoa 桥接转换有点难以阅读,因此我个人认为跳过 Cocoa 级别并直接在 CF 级别上创建查询字典会更干净,如下所示:
CFMutableDictionaryRef cfquery = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(cfquery, kSecClass, kSecClassIdentity);
CFDictionarySetValue(cfquery, kSecReturnRef, kCFBoolenTrue);
CFDictionarySetValue(cfquery, kSecMatchLimit, kSecMatchLimitAll);
CFArrayRef cfidentities = NULL;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)cfquery, (CFTypeRef *)&cfidentities);
CFRelease(cfquery);
if (status == errSecSuccess) {
NSArray *identities = CFBridgingRelease(cfidentities);
for (id value in identities) {
SecCertificateRef cfcertificate;
SecIdentityRef cfidentity = (SecIdentityRef)CFBridgingRetain(value);
status = SecIdentityCopyCertificate(cfidentity, &cfcertificate);
if (status == errSecSuccess) {
// ...
CFRelease(cfcertificate);
}
}
}
关于iphone - SecItemCopyMatching 在 ARC 下的 osx 上仍然存在泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16780202/
我知道您可以使用 -fno-objc-arc 标志对 ARC 项目中不支持 ARC 的文件禁用 ARC。 有什么方法可以在非 ARC 项目中为支持 ARC 的文件启用 ARC 吗? 谢谢! 最佳答案
ARC 代表自动引用计数。 我刚刚升级了我的项目以使用 ARC。虽然不是全部。只是一些。 我怎么知道? 最佳答案 要查看默认值,您必须检查目标属性(您的目标 -> build设置 -> Apple L
我有一个框架 A,它是 ARC。这会消耗一些来自非 ARC 框架 B 的 API。 框架B示例代码(非ARC): @interface Settings : NSObject { NSDict
我一直避免升级 Xcode,直到某个特定项目完成(即将推出)。我希望能够在未来的项目中使用 ARC,同时保留维护和调查先前软件产品问题的能力。 问题是可以通过安装一次 Xcode 来完成此操作吗?当我
我正在开发一个非常旧的非基于 ARC 的项目,并添加了一些基于 ARC 的新 UIViewController(可以通过在构建阶段设置 -fobjc-arc 标志来实现) . 由于ARC和非ARC的混
这个问题在这里已经有了答案: How do you enable ARC project-wide in Xcode 4.2 (3 个答案) How to automatically conver
我是 iPhone 开发的新手。我了解到ARC是现在IOS引入的新特性。我的问题是,我有一个手动编写版本的旧非 ARC 项目,是否可以将该项目从非 ARC 切换到 ARC ? 谢谢, 拉吉 最佳答案
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
我的项目是在 ARC 打开的情况下创建的,现在我使用的是第三方框架,该框架很可能不是用 ARC 构建的。这样做会有什么问题吗?我现在遇到了框架中某个地方发生的崩溃,但我没有得到有关崩溃的太多详细信息。
我猜这是一个简单的问题,但我无法弄清楚: 我的项目中存在三个与构建阶段中设置的链接器标志 -fno-obj-arc 不 ARC 兼容的文件。 但是据我所知,这些标志被忽略了。编译器仍然提示向对象发送保
我认为标题很好地解释了我的问题,我目前正在开发一个满足我个人需求的小型框架,并考虑使用 ARC 构建它(如果可能的话?),并在旧项目中使用它是在 ARC 之前 build 的吗? 最佳答案 是的,但有
我正在努力让现有的启用 ARC 的控件在不启用 ARC 的项目中在 Xcode 4.2 (OSX Snow Leopard) 下运行,并且我面临着各种问题: 我应该如何解决以下问题? 在属性中使用(s
我想知道如何更改 Arc 中的值,然后使 Arc 的其他副本再次有效。 use std::sync::Arc; use std::thread; use std::error::Error; use
ARC 运行时版本:我不知道,但我在这个网站上下载了它,https://chrome.google.com/webstore/detail/arc-welder/emfinbmielocnlhgmfk
当我将 iPhone 项目转换为 Objective - c ARC 时,我总是遇到相同的错误: /Applications/Xcode.app/Contents/Developer/Platform
如何在没有 ARC 的情况下为使用 ARC 的项目构建静态库? 我找到了一些静态库的源代码,我想为我的项目构建它,但我使用的是 ARC。 最佳答案 静态库可以在没有 ARC 的情况下构建,并且可以使用
我有一个使用 ARC 的 A 类和不使用 ARC 的其他 B 类和 C 类。如果 A 包含 B 类对象而 B 包含 C 类对象,那么 ARC 如何工作以及当 B 类和 C 类中的内存管理处理不当时会发
一旦我将 -fno-objc-arc 标志添加到构建阶段中的一个或多个单独的文件,我的 MyProject-Prefix.pch 文件就会进入非 ARC 模式,导致 150 条警告,如“警告:没有‘分
在某些情况下,我需要为几个非 ARC xcode 项目中的许多文件启用 ARC。我不想完全转换为 ARC,只需要使用 arc 启用几个文件。 有许多解决方案使用 XCode 控制台将 -fobjc-a
我还没有使用过 ARC,只是在它通过第 3 方代码强制进入项目时处理它。我已经阅读了所有 ARC 文档,但还没有看到这个问题的答案: 如果我在使用 -fobjc-arc 编译的模块中定义了一个类,我能
我是一名优秀的程序员,十分优秀!