SecItemCopyMatching 内存泄漏

发布于 2024-10-12 13:02:55 字数 1485 浏览 1 评论 0原文

我在下一个代码中出现内存泄漏。我的灵感来自此处,这是一个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 技术交流群。

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

发布评论

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

评论(1

你的呼吸 2024-10-19 13:02:55

您的方法有时返回一个保留计数+1的实例,并且您很可能不会在代码的其余部分中释放它。如果调用 SecItemCopyMatching,您将返回保留计数 +1,但如果设置了 publicKey,则您的函数将返回保留计数 +-0 的值,这是不好的。

您需要确保始终以相同的保留计数返回。在这种情况下,我会这样做:

} else {
    publicKeyReference = publicKey;
    CFRetain(publicKeyReference);
}

然后,您的方法的每个调用者都必须确保 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:

} else {
    publicKeyReference = publicKey;
    CFRetain(publicKeyReference);
}

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.

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