Memory leakage?


I’m wondering if writing this code example different would result in a leak. On page 235 take this excerpt for example:
(ignore the dot notation)

CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
possession.imageKey = (NSString *)newUniqueIDString;

To me, I would be inclined to write this as:

CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, CFUUIDCreate(kCFAllocatorDefault));
possession.imageKey = (NSString *)newUniqueIDString;

Would the old “newUniqueID” be a leak? How would it get released since it’s not being stored anywhere?

Since that was written late at night, I think a good nights sleep I was able to answer my own question. Correct me if I’m wrong, but this doesn’t create a leak because like I said in my original post, it’s not stored in any variable, so no variable was ever created (except the one used in the method, which should be released at the end of the method. Again, correct me if I’m wrong.


It would be a leak. Anytime you create an object (alloc, new, copy, or a C function with Create in it), that object is allocated - regardless of if you have a variable that points to it. In fact, creating an object without having a variable to point to it is guaranteed to be a leak because you have no way of releasing it later.