SecItemCopyMatching 内存泄漏
我在下一个代码中出现内存泄漏。我的灵感来自此处,这是一个RSA算法的一部分。
- (SecKeyRef)getPublicKeyRef {
OSStatus resultCode = noErr;
SecKeyRef publicKeyReference = NULL;
if(publicKey == NULL) {
NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];
NSData *publicTag = [NSData dataWithBytes:publicKeyIdentifier
length:strlen((const char *)publicKeyIdentifier)];
// Set the public key query dictionary.
[queryPublicKey setObject:(id)kSecClassKey forKey:(id)kSecClass];
[queryPublicKey setObject:publicTag forKey:(id)kSecAttrApplicationTag];
[queryPublicKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
[queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnRef];
// Get the key.
resultCode = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyReference);
// NSLog(@"getPublicKey: result code: %d", resultCode);
if(resultCode != noErr)
{
publicKeyReference = NULL;
}
// [publicTag release];
[queryPublicKey release];
} else {
publicKeyReference = publicKey;
}
return publicKeyReference;
泄漏仪器
显示此行发生泄漏:
resultCode = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyReference);
请告诉我如何解决此问题。
I have a memory leak in the next code. I inspired from here and this is a part of RSA algorithm.
- (SecKeyRef)getPublicKeyRef {
OSStatus resultCode = noErr;
SecKeyRef publicKeyReference = NULL;
if(publicKey == NULL) {
NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];
NSData *publicTag = [NSData dataWithBytes:publicKeyIdentifier
length:strlen((const char *)publicKeyIdentifier)];
// Set the public key query dictionary.
[queryPublicKey setObject:(id)kSecClassKey forKey:(id)kSecClass];
[queryPublicKey setObject:publicTag forKey:(id)kSecAttrApplicationTag];
[queryPublicKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
[queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnRef];
// Get the key.
resultCode = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyReference);
// NSLog(@"getPublicKey: result code: %d", resultCode);
if(resultCode != noErr)
{
publicKeyReference = NULL;
}
// [publicTag release];
[queryPublicKey release];
} else {
publicKeyReference = publicKey;
}
return publicKeyReference;
}
The Leak instrument says it is leaking in this line:
resultCode = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyReference);
Please tell me how could I solve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方法有时返回一个保留计数+1的实例,并且您很可能不会在代码的其余部分中释放它。如果调用 SecItemCopyMatching,您将返回保留计数 +1,但如果设置了 publicKey,则您的函数将返回保留计数 +-0 的值,这是不好的。
您需要确保始终以相同的保留计数返回。在这种情况下,我会这样做:
然后,您的方法的每个调用者都必须确保
CFRelease
该值...但这会违反 get 规则(它应该return keep count +-0),所以重命名该方法也许是个好主意。Your method is sometimes returning an instance with retain count +1 and you are most likely not releasing it in the rest of your code. You are returning with retain count +1 if SecItemCopyMatching is called, but if publicKey is set then your function is returning a value with retain count +-0 which is bad.
You need to make sure that you always return with the same retain count. In this case, I'd do:
Then every caller to your method must make sure to
CFRelease
the value... but that'd violate the get rule (it should return retain count +-0), so maybe it would be a good idea to then rename the method.