以下单例类(SharedManager)辅助方法可能导致保留周期。在静态分析器中获取警告:“在线路上分配的对象的潜在泄漏......”我该如何解决?
我确实尝试过使用ivar uuid __weak,但在分析时仍会出现警告。
NSString *__weak uuid = (__bridge NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
由于
像在课堂上一样被召唤:
myUUID = [SharedManager generateUUID];
+ (NSString *)generateUUID
{
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuid = (__bridge NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
CFRelease(uuidObject);
return uuid;
}
答案 0 :(得分:8)
以下是释放它们的方法:
- (NSString *) uuid
{
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
NSString *uuid = [NSString stringWithString:(NSString *)
uuidStringRef];
CFRelease(uuidStringRef);
return uuid;
}
来源:http://www.cocoabuilder.com/archive/cocoa/217665-how-to-create-guid.html
答案 1 :(得分:6)
NSString *uuid = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
是否会删除警告?