CFUUIDBytes可能导致iOS内存泄漏(Potential iOS memory leak with CFUUIDBytes)
我有一个静态方法定义如下:
+(NSString*)getNewIdAsBase64 { // Grab new uuid CFUUIDRef originalUuid = CFUUIDCreate(nil);//create a new UUID //NSLog(@"Step a: original uuidObj = %@",originalUuid); CFUUIDBytes originalBytes = CFUUIDGetUUIDBytes (originalUuid);//ISSUE NSData* data88 = [NSData dataWithBytes: &originalBytes length: sizeof(originalBytes)]; NSString* hugo = [data88 base64EncodedString]; return hugo; }当我在我的项目上运行Product-> Analyze时,使用// ISSUE注释的行会引发以下问题:
Potential leak of an object alocated on line 23 and stored into 'originalUuid'我做错了什么,我该如何解决?
谢谢
I have a static method defined as follows:
+(NSString*)getNewIdAsBase64 { // Grab new uuid CFUUIDRef originalUuid = CFUUIDCreate(nil);//create a new UUID //NSLog(@"Step a: original uuidObj = %@",originalUuid); CFUUIDBytes originalBytes = CFUUIDGetUUIDBytes (originalUuid);//ISSUE NSData* data88 = [NSData dataWithBytes: &originalBytes length: sizeof(originalBytes)]; NSString* hugo = [data88 base64EncodedString]; return hugo; }When I run Product->Analyze on my project then the line commented with //ISSUE raises the following issue:
Potential leak of an object alocated on line 23 and stored into 'originalUuid'What am I doing wrong, and how do I fix it?
Thanks
最满意答案
即使您使用ARC,也需要释放您创建的对象:
+(NSString*)getNewIdAsBase64 { // Grab new uuid CFUUIDRef originalUuid = CFUUIDCreate(nil);//create a new UUID //NSLog(@"Step a: original uuidObj = %@",originalUuid); CFUUIDBytes originalBytes = CFUUIDGetUUIDBytes (originalUuid);//ISSUE NSData* data88 = [NSData dataWithBytes: &originalBytes length: sizeof(originalBytes)]; NSString* hugo = [data88 base64EncodedString]; CFRelease(originalUuid); return hugo; }You will need to release the object you create even if you are using ARC:
+(NSString*)getNewIdAsBase64 { // Grab new uuid CFUUIDRef originalUuid = CFUUIDCreate(nil);//create a new UUID //NSLog(@"Step a: original uuidObj = %@",originalUuid); CFUUIDBytes originalBytes = CFUUIDGetUUIDBytes (originalUuid);//ISSUE NSData* data88 = [NSData dataWithBytes: &originalBytes length: sizeof(originalBytes)]; NSString* hugo = [data88 base64EncodedString]; CFRelease(originalUuid); return hugo; }更多推荐
发布评论