【问题标题】:Keychain access code leaking钥匙串访问代码泄漏
【发布时间】:2012-09-05 16:12:08
【问题描述】:

这是泄漏:

这是我的代码:

+(NSString *)getSecureValueForKey:(NSString *)key {

    // Retrieve a value from the keychain
    NSDictionary *result;
    NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecReturnAttributes, nil] autorelease];
    NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, kCFBooleanTrue, nil] autorelease];
    NSDictionary *query = [[NSDictionary alloc] initWithObjects: objects forKeys: keys];

    // Check if the value was found
    OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, (CFTypeRef *) &result);
    [query release];
    if (status != noErr) {
        // Value not found
        return nil;
    } else {
        // Value was found so return it
        NSString *value = (NSString *) [result objectForKey: (NSString *) kSecAttrGeneric];
        return value;
        [result release];
    }
}

+(BOOL)storeSecureValue:(NSString *)value forKey:(NSString *)key {
    // Get the existing value for the key
    NSString *existingValue = [self getSecureValueForKey:key];

    // Check if a value already exists for this key
    OSStatus status;
    if (existingValue) {
        // Value already exists, so update it
        NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, nil] autorelease];
        NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, nil] autorelease];
        NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
        status = SecItemUpdate((CFDictionaryRef) query, (CFDictionaryRef) [NSDictionary dictionaryWithObject:value forKey: (NSString *) kSecAttrGeneric]);
    } else {
        // Value does not exist, so add it
        NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecAttrGeneric, nil] autorelease];
        NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, value, nil] autorelease];
        NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
        status = SecItemAdd((CFDictionaryRef) query, NULL);
    }

    // Check if the value was stored
    if (status != noErr) {
        // Value was not stored
        return false;
    } else {
        // Value was stored
        return true;
    }
}

你能帮我解决它吗?每次我在钥匙串中访问或存储数据时都会发生泄漏。 我已经很久没有在没有 ARC 的情况下进行编程了,我就是无法追踪这个泄漏!

谢谢!

【问题讨论】:

  • return value;之前做[result release];

标签: iphone xcode cocoa-touch memory-leaks


【解决方案1】:

在创建query之后释放keysobjects,在返回value之前释放result之前,实际上释放在你的存储安全值方法期间创建的新对象。

此时,您的选择是,了解内存管理的实际工作原理,或者只是打开 ARC 并让它为您处理大部分工作。

【讨论】:

  • keysobjects 是自动释放的。我宁愿手动释放它们,但它们仍然不会泄漏。
猜你喜欢
  • 2016-01-17
  • 1970-01-01
  • 2020-06-20
  • 2020-01-26
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多