Sunil Parmar
Sunil Parmar

Reputation: 1233

How to resolve error of "Reference counted object is used after it is released"?

I am getting following error but how to resolve it ?
Error is highlighted with green circle "Reference counted object is used after it is released"

Edited: I am using following method

+ (NSString *)GetUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    NSString *str = (__bridge NSString *)string;
    CFRelease(string);
    return str;
}


Edited: Resolved by using vijay's following simple code

NSUUID  *UUID = [NSUUID UUID];
NSString* stringUUID = [UUID UUIDString];


enter image description here

Upvotes: 1

Views: 108

Answers (2)

I hope, you are getting this error because of [DBManager GetUUID] method, where you would release the CFRelease(cfUuid).

To get the UUID, try this simplified API

+ (NSString *)GetUUID
{

    NSUUID  *UUID = [NSUUID UUID];
    NSString* stringUUID = [UUID UUIDString];

    return stringUUID;

}

Upvotes: 1

Thomas Deniau
Thomas Deniau

Reputation: 2573

After CFUUIDCreateString, you get a string you own. By using __bridge, you set str to the same string. So when you CFRelease(string) you do not own the memory backing str anymore...

To avoid this, either use a Cocoa method like @vijay says, or remove the CFRelease and use __bridge_transfer NSString* instead of __bridge. This tells the compiler you're transferring a CF object you own into the ARC world.

Per the documentation:

__bridge_transfer or CFBridgingRelease moves a non-Objective-C pointer to Objective-C and also transfers ownership to ARC. ARC is responsible for relinquishing ownership of the object.

Upvotes: 0

Related Questions