使用CFUUIDBytes可能导致iOS内存泄漏

时间:2012-10-17 14:56:28

标签: objective-c ios memory-leaks

我有一个静态方法,定义如下:

+(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'

我做错了什么,我该如何解决?

由于

1 个答案:

答案 0 :(得分:4)

即使您使用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;

}