简体   繁体   中英

Fix potential memory leak in ARC

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;

}

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

NSString  *uuid =  (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);

这会删除警告吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM