Fix potential memory leak in ARC

2019-04-07 15:02发布

The following singleton class (SharedManager) helper method might be causing a retain cycle. Getting warnings in static analyzer: "Potential leak of an object allocated at line ..." How can I fix?

I did try making ivar uuid __weak but warning still appears when I analyze.

    NSString  *__weak uuid =  (__bridge NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);

Thanks

Being called in the class like so:

myUUID = [SharedManager generateUUID];



+ (NSString *)generateUUID
{

 CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
 NSString  *uuid =  (__bridge NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
CFRelease(uuidObject);

  return uuid;

}

2条回答
啃猪蹄的小仙女
2楼-- · 2019-04-07 15:38

Here is a way to release them:

- (NSString *) uuid
{
    CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    NSString *uuid = [NSString stringWithString:(NSString *)
    uuidStringRef];
    CFRelease(uuidStringRef);
    return uuid;
}

Source: http://www.cocoabuilder.com/archive/cocoa/217665-how-to-create-guid.html

查看更多
够拽才男人
3楼-- · 2019-04-07 15:49
NSString  *uuid =  (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);

Does that remove the warning?

查看更多
登录 后发表回答