I think I learned something today about Objective C properties and retain. It seems that in a method where you reference a property, you need to use the form “
self.property_name” if you want the assigned object to be automagically retained. If you simply say “
property_name = some_object” it doesn’t retain some_object!
Witness the source code and the associated results:
Source: Retain Release main.m
2008-12-22 20:52:18.824 RetainRelease[3623:10b] Before assignment: [a retainCount] = 1 2008-12-22 20:52:18.837 RetainRelease[3623:10b] After assignment: [a retainCount] = 1 2008-12-22 20:52:18.838 RetainRelease[3623:10b] Before assignment: [b retainCount] = 1 2008-12-22 20:52:18.839 RetainRelease[3623:10b] After assignment: [b retainCount] = 2 2008-12-22 20:52:18.842 RetainRelease[3623:10b] Final [oo retainCount] = 2
Seems to me proof that when you’re assigning to them, you should always reference properties on an object by prefixing them with “
Caveat: Inside of the setter method for a property, do NOT use the “
self.property” syntax. Otherwise, you end up in a infinite loop. Ask me how I know. This is the best (and only) argument I can see for having a different behavior between the “
self.property =” and “
property =” usages.