Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problems is here:</p> <pre><code> CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL)); deviceUuid = (NSString *)cfUuid; CFRelease(cfUuid); [defaults setObject:deviceUuid forKey:@"deviceUuid"]; </code></pre> <p>Let's go through what this actually does:</p> <pre><code> CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL)); </code></pre> <p>A CFUUID is created (and leaked). A CFStringRef is created and assigned to cfUuid. (Note: The name cfUuid implies that cfUuid is a CFUUIDRef. Of course, it isn't; it's a CFStringRef.)</p> <pre><code> deviceUuid = (NSString *)cfUuid; </code></pre> <p>That same CFStringRef is <em>type cast</em> and assigned to deviceUuid. This is <strong>not</strong> a new instance of NSString or CFStringRef, it's just a typecast of the <strong>same</strong> instance.</p> <pre><code> CFRelease(cfUuid); </code></pre> <p>You release the CFStringRef. Since the NSString points to the same object, you also release it.</p> <pre><code> [defaults setObject:deviceUuid forKey:@"deviceUuid"]; </code></pre> <p>And here, you use the typecasted object, which was released before.</p> <p>THe simplest fix to the stale pointer is this:</p> <pre><code> CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL)); deviceUuid = (NSString *)cfUuid; [defaults setObject:deviceUuid forKey:@"deviceUuid"]; CFRelease(cfUuid); </code></pre> <p>But this code is dangerous, and you already know why: deviceUuid is also invalid. But this isn't obvious, so you can trip on it later. Also, it doesn't fix the CFUUID leak.</p> <p>To fix the CFStringRef leak, you could use this:</p> <pre><code> deviceUuid = (NSString *)CFUUIDCreateString(NULL, CFUUIDCreate(NULL)); [defaults setObject:deviceUuid forKey:@"deviceUuid"]; [deviceUuid autorelease]; // or release, if you don't need it in code not // included in your post </code></pre> <p>However, this still doesn't fix the CFUUID leak.</p> <pre><code> CFUUIDRef cfuuid = CFUUIDCreate(NULL); deviceUuid = (NSString *)CFUUIDCreateString(NULL, cfuuid); CFRelease(cfuuid); [defaults setObject:deviceUuid forKey:@"deviceUuid"]; [deviceUuid autorelease]; // or release, if you don't need it in code not // included in your post </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload