对象在目标C中泄露

时间:2016-08-23 06:22:29

标签: ios objective-c memory-leaks uuid

当我做"构建和分析" XCode给了我以下警告:

存储在'字符串'

中的对象的潜在泄漏
- (NSString *)getUUID
{
NSString *UUID = [EA_APP_CONSTANT getUserDefaultsValueForKey:@"uniqueID"];
if (!UUID) {
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    UUID = [(__bridge NSString*)string stringByReplacingOccurrencesOfString:@"-"withString:@""];
    [EA_APP_CONSTANT setUserDefaultsValue:UUID forKey:@"uniqueID"];
}
return UUID;
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您需要致电CFRelease(string);

- (NSString *)getUUID
{
NSString *UUID = [EA_APP_CONSTANT getUserDefaultsValueForKey:@"uniqueID"];
if (!UUID) {
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    UUID = [(__bridge NSString*)string stringByReplacingOccurrencesOfString:@"-"withString:@""];
    [EA_APP_CONSTANT setUserDefaultsValue:UUID forKey:@"uniqueID"];

    CFRelease(string);
}
return UUID;
}