Reputation: 77
If I have a function like this
void setSomeObject( SomeObjectClass obj /*, and some other params*/ )
{
[_previous autorelease];
_previous = obj;
}
As far as I understood it the autorelease message is sent to the object itself (not _previous) so at one point, sometime when setSomeObject goes out of scope the original object is autoreleased (if any). Is this correct? I am not using properties but I guess by using them the release of the previous object would be automatic when I do self.previous = obj; ?
Upvotes: 1
Views: 77
Reputation: 118731
When you send an -autorelease
message to an object, it's added to the active NSAutoreleasePool
, which is emptied when the run loop runs. If you say [_previous autorelease]
, only that object will be autoreleased, and if you then say _previous = obj
, that only changes the variable's reference. The old object is still autoreleased.
If you're doing this in a setter method, this is what the pattern generally is:
- (void)setSomeObject:(MyObjClass *obj) {
if (obj != someObject) {
[someObject release];
someObject = [obj retain]; // or copy, depending on what you want
}
}
Upvotes: 1
Reputation: 5732
No that is incorrect.
[_previous autorelease] sends the autorelease message to _previous. That is the meaning of this syntax.
Upvotes: 0