self.m_Object = [[SomeObject alloc] init];
This causes an over-retain. You get one claim of ownership for the alloc
, and another through the setter, which is declared as retain
ing the new value. Since you only have one pointer to the new value, you have too many claims. When the setter is used again:
self.m_Object = anotherObject;
the original object will receive only one release
message, and you will lose the pointer. Since you had two claims on the object, it will not be deallocated, and you will have a leak.
The property access: self.m_Object = obj;
is translated by the compiler into [self setM_Object:obj];
. That is, the setter method for the property, created by the @synthesize
directive, is called. That setter method retains its argument. The other option is to use the ivar directly in your init
method:
-init {
//...
m_Object = [[SomeObject alloc] init];
//...
}
then you have only one claim on this object, caused by the use of alloc
. Since you also have one reference to the object, this is correct.