3

I am creating a NSString like this, should I release it afterwards or not?

CFUUIDRef   uuidObj = CFUUIDCreate(nil);
        NSString *device = (NSString*)CFUUIDCreateString(nil, uuidObj);
        CFRelease(uuidObj);

Thanks!

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
  • 1
    hmm.. the convention is that when you do alloc or have *Alloc* in your method, you need to release them. – Kenny Lim Sep 10 '11 at 22:25
  • I am allocing it here correct? I don't see the word alloc anywhere but I am assuming I am allocing it right? – SimplyKiwi Sep 10 '11 at 22:29
  • 1
    if no "alloc" in the method, you didn't alloc then. you don't have to call release on those objects. EDIT: in addition to alloc, there are new, copy, mutableCopy. I am including the Apple Memory Management Guide as Answer. – Kenny Lim Sep 10 '11 at 22:41
  • 6
    Kenny, this is an Core Foundation function falling under the Core Foundation memory guidelines which says that the keyword "create" in fact does delegate the ownership back to the caller! – JustSid Sep 10 '11 at 22:59

2 Answers2

7

Here is documentation for this function:

CFUUID Reference - CFUUIDCreateString

and it states that ownership follows The Create Rule.

Here are some more links with answer:

Robert Vuković
  • 4,677
  • 6
  • 31
  • 47
  • Thanks for all the info and help! So your saying this code: device = [(NSString *)CFUUIDCreateString(NULL, CFUUIDCreate(NULL)) autorelease]; Will do the same as my code in my original post? – SimplyKiwi Sep 10 '11 at 22:41
  • 1
    The comment to use NULL instead of 0 was to the original poster on the first link, not to you. :) Here are some links for the NULL and nil differences. http://stackoverflow.com/questions/1564410/when-to-use-nil-and-null-in-objective-c , http://stackoverflow.com/questions/2461617/are-null-and-nil-equivalent – Robert Vuković Sep 14 '11 at 07:18
6
CFUUIDRef theUUID = CFUUIDCreate(NULL);

NSString *s2ndUuid = (__bridge_transfer NSString*)CFUUIDCreateString(kCFAllocatorDefault, theUUID);
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
user1086279
  • 101
  • 1
  • 4