0

When 'Analyse', Got warning "Incorrect decrement of the reference count of an object that is not owned at this point by the caller" in this. (xCode 4.1+)

I can understant that it's better to "kill" an object from it's proper class, but is there a way to do it from outside without a warning?

Thanks!

NSLog(@"--[%s:%d]",__PRETTY_FUNCTION__, __LINE__);
myAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
   if (appDelegate.referencesPopup != nil) {
        [appDelegate.referencesPopup release]; // warning
        appDelegate.referencesPopup = nil;  
   }      
}
Vladimir
  • 170,431
  • 36
  • 387
  • 313
Franck
  • 8,939
  • 8
  • 39
  • 57

2 Answers2

2

Just set the property to nil, there is no need to release it here, as the analyzer is telling you. If you didn't retain it, don't release it.

Also, you shouldn't call release on something that is returned by a dot notation getter. This has been discussed extensively here: Why shouldn't I use the getter to release a property in objective-c?

Community
  • 1
  • 1
jrturton
  • 118,105
  • 32
  • 252
  • 268
0

Most likely, the way in which you are acquiring the object being stored in referencesPopup, you did not allocate it yourself or retain it. In that case, you are not responsible for releasing it. You can simply set the ivar to nil.

Mark Granoff
  • 16,878
  • 2
  • 59
  • 61
  • Hi. Thanks, but i'm still confused. If I reallocate the object after the appDelegate.referencePopUp=Nil, I will create a memory leak, no? (referencePupUp =nil doesn't means that retaincount decrement) – Franck Oct 12 '11 at 13:44
  • How is `referencesPopup` assigned initially? Do you allocate an object or acquire an object from somewhere else? If the latter, then setting `referencesPopup` to nil is harmless; if you did not allocate (or copy) an object then you are not responsible for releasing it. – Mark Granoff Oct 12 '11 at 14:39
  • appDelegate.referencesPopup = [[References alloc] init]; – Franck Oct 12 '11 at 14:41
  • Ok, so you are allocating it, and because you are using the setter method to access `referencesPopup`, you're getting a `retain` done for you. So, in your code above, you do NOT need to call release yourself because by using the setter method again to set `referencesPopup` to nil, you get a `release` done for you. So that is why your call to release is redundant. – Mark Granoff Oct 12 '11 at 15:07
  • appDelegate.referencesPopup = [[References alloc] init] Hi, based on your comments, this can have a memory leak, no? [[References alloc] init] = retaincout++ and appDelegate.referencesPopup = retaincount++ (user the setter =implicit retain) So, correct line should be (?) appDelegate.referencesPopup = [[References alloc] init]autorelease]; Still starting to understand, and ARC comings... :) – Franck Oct 13 '11 at 13:44
  • Although it seems more efficient to combine the assignment of a variable (through a setter, here) with the allocation and init of an object, that is actually the wrong way to do it (without ARC). You should have 3 distinct statements: `References *r = [[References alloc] init]`, `appDelegate.referencesPopup = r`, and `[r release]`. The middle statement invokes a setter method and you get a `retain` for free. Using autorelease in this mix would be wrong as well. – Mark Granoff Oct 13 '11 at 14:41