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);
Does that remove the warning?
Related
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];
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;
}
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.
-(void)displayData:(NSString *)text{
NSLog(#"DATA SEND");
NSString *string1 = [NSString stringWithFormat:text];
NSString *separate = [string1 componentsSeparatedByString:#"B"];
separate is the one that gives issue? How do I properly do this? I'm trying to perform a string split.
componentsSeparatedByString: method returns NSArray not NSString, try that:
NSArray *seperate = [string1 componentsSeparatedByString:#"B"];
what is that?
NSString *string1 = [NSString stringWithFormat:text];
Right way
NSString *string1 = [NSString stringWithFormat:#"%#",text];
or
NSString *string1 = [NSString stringWithString:text];
and then Grag answer.
I want to implement UUID v1 in my iOS App.
I know that it is composed of Mac Address and timestamp as described in
http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_.28MAC_address.29
Is there any objective-c implementation for this V1, based on CFUUID functions ?
I already have the mac address and the timestamp.
The UUID v1 description at Wikipedia : "The original (version 1) generation scheme for UUIDs was to concatenate the UUID version with the MAC address of the computer that is generating the UUID, and with the number of 100-nanosecond intervals since the adoption of the Gregorian calendar in the West"
It is also specified at http://www.ietf.org/rfc/rfc4122.txt , but it seems that it will need time to implement it.
I have found this link : http://www.famkruithof.net/guid-uuid-timebased.html who have a simple explanation for the steps to create a v1 UUID. Is there any existing implementation, before I implement it by my self?
I thinks it is common behavior to use framework functions. And that is use CFUUID. For example:
+(NSString*)get {
NSString *deviceID = [[NSUserDefaults standardUserDefaults] objectForKey:#"DeviceID"];
if (!deviceID) {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
deviceID = (NSString*)string;
[[NSUserDefaults standardUserDefaults] setValue:deviceID forKey:#"DeviceID"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return deviceID;
}
Please try this one. It may be helpful to you
+(NSString*) Create_UDID
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
NSString* strString = [NSString stringWithFormat:#"%#", string];
NSString *strValue = [strString stringByReplacingOccurrencesOfString:#"-"withString:#""];
if (strValue == nil) {
strValue = #"";
}
return strValue;
}
How to get a UUID in objective c, like in Java UUID is used to generate unique random numbers which represents 128 bit value.
Try:
CFUUIDRef udid = CFUUIDCreate(NULL);
NSString *udidString = (NSString *) CFUUIDCreateString(NULL, udid);
UPDATE:
As of iOS 6, there is an easier way to generate UUID. And as usual, there are multiple ways to do it:
Create a UUID string:
NSString *uuid = [[NSUUID UUID] UUIDString];
Create a UUID:
[NSUUID UUID]; // which is the same as..
[[NSUUID] alloc] init];
Creates an object of type NSConcreteUUID and can be easily casted to NSString, and looks like this: BE5BA3D0-971C-4418-9ECF-E2D1ABCB66BE
NOTE from the Documentation:
Note: The NSUUID class is not toll-free bridged with CoreFoundation’s CFUUIDRef. Use UUID strings to convert between CFUUID and NSUUID, if needed. Two NSUUID objects are not guaranteed to be comparable by pointer value (as CFUUIDRef is); use isEqual: to compare two NSUUID instances.
Swift version of Raptor's answer:
let uuid = UUID().uuidString
+ (NSString *)uniqueFileName
{
CFUUIDRef theUniqueString = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUniqueString);
CFRelease(theUniqueString);
return [(NSString *)string autorelease];
}
-(NSString*) myUUID()
{
CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
NSString *guid = (__bridge NSString *)newUniqueIDString;
CFRelease(newUniqueIDString);
CFRelease(newUniqueID);
return([guid lowercaseString]);
}
you can use CFUUID for iOS 5 or lower version and NSUUID for iOS 6 and 7.
for making it more secure you can store your UUID in keychain
- (NSString*)generateGUID{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return [NSString stringWithFormat:#"%#", string];
}
For Swift 5.0, Use this,
let uuidRef = CFUUIDCreate(nil)
let uuidStringRef = CFUUIDCreateString(nil, uuidRef)
let uuid = uuidStringRef as String? ?? ""
Still learning iOS development with ObjectiveC and iOS, and trying to realy understand memory management! Appreciate any advise on the snippet below, eg:
1) Analyser says there are potential memory leaks, but can't solve them?
2) Should I keep alloc and init the NSStrings in the for loop and when appended to?
Thanks
- (NSString *) lookUpCharNameForID: (NSString *) inCharID
{
debugPrint ("TRACE", [[#"Lookup Char Name for = " stringByAppendingString: inCharID] UTF8String]);
NSString *tempName = [[NSString alloc] initWithFormat: #""];
if (![inCharID isEqualToString: #""])
{
// Potentially lookup multiple values
//
NSString *newName = [[NSString alloc] initWithFormat: #""];
NSArray *idList = [inCharID componentsSeparatedByString: #","];
for (NSString *nextID in idList)
{
NSLog( #"Lookup %i : %#", [idList count], nextID);
newName = [[NSString alloc] initWithFormat: #"C%#", nextID];
// Append strings
if ([tempName isEqualToString: #""])
tempName = [[NSString alloc] initWithFormat: #"%#", newName];
else
tempName = [[NSString alloc] initWithFormat: #"%#+%#", tempName, newName];
}
[newName release];
}
return [tempName autorelease];
}
You don't need any of the calls to alloc, release, or autorelease. Instead, use [NSString stringWithFormat:] to create instances of NSString that you don't own, and therefore don't need to manage. Also, consider using NSMutableString to simplify your code a bit, for example along the lines of the following (untested) version:
- (NSString *) lookUpCharNameForID: (NSString *) inCharID
{
NSMutableString *tempName = nil;
if (![inCharID isEqualToString: #""])
{
NSArray *idList = [inCharID componentsSeparatedByString: #","];
for (NSString *nextID in idList)
{
[tempName appendString:#"+"]; // Does nothing if tempName is nil.
if (tempName == nil)
tempName = [NSMutableString string];
[tempName appendFormat:#"C%#", nextID];
}
}
return tempName;
}
You have 2 alloc initWithFormat for tempName. One before the loop and one within the loop.
Use ARC (Automatic Reference Counting) for new projects. For older projects it may be easy to convert them, if not ARC can be disabled on a file-by-file basis where necessary.
Using a mutable string, autoreleased convience methods and a little rerfactoring:
- (NSString *) lookUpCharNameForID: (NSString *) inCharID
{
NSMutableString *tempName = [NSMutableArray array];
if (inCharID.length)
{
NSArray *idList = [inCharID componentsSeparatedByString: #","];
for (NSString *nextID in idList)
{
if (tempName.length == 0)
[tempName appendFormat: #"%#C", nextID];
else
[tempName appendFormat: #"+%#C", nextID];
}
}
return tempName;
}