views:

45

answers:

3

I'm doing a set method:

OBS: somobject is an attribute of a class.

– (void)setSomeObject:(SomeObject *)newSomeobject {

    [someobject autorelease];

    someobject = [newSomeobject retain];

    return;

}

on [somobject autorelease] I declare that I don't want more to own the object under the scope of setSomeObject.

Does the "someobject" retained by another object will be released? Or the object will be released just on setSomeObject method?

If the someobject class atribute already exists?

What will be the behavior of this object?

+1  A: 

You have a significant problem, in that it seems you have two variables (the method parameter and an instance variable) with the same name. The compiler (and readers of this question, for that matter) can't tell to which you're referring.

For your memory management problems, check out Apple's programming guide.

Graham Lee
Ok I've read it and understood. Thanks.The someobject will be released and the atribute will be set with another attribute and retain is called on newSomeObject to ensure the object ownership.
okami
+1  A: 

I'd rename the parameter in the method so that it's different from the ivar:

– (void)setSomeObject:(SomeObject *)newObject 
{
    [someobject autorelease];

    someobject = [newObject retain];
}

Also you should read Apple docs for memory management and @property and @synthesize.

Benedict Cohen
A: 

What you need to accomplish in a setter is:

  1. Release any old object
  2. Retain the new object
  3. Assign the new object to your instance variable

Of course, if you do it literally in that order, you risk releasing the object too soon in the case where old & new objects are the same. That's where the "autorelease" comes in handy, because it schedules the object to be released, but only after your method returns.

Naming the method parameter & instance variable the same is (IMHO) confusing, and will give you a compiler warning, but if you absolutely insist on doing it that way, you can use "self->" to specify that you're referring to the instance variable:

– (void)setSomeObject:(SomeObject *)someobject {

[self->someobject autorelease];

self->someobject = [someobject retain];

return;

}

Finally, unless your setter method has to do something special, you should consider using @synthesize to have your setter/getter automatically generated.

David Gelhar
David, why do you put return; I also saw this on many examples and didn't understand, if the method will return why do you call return?
okami
@okami no good reason. I copied/pasted your original code and neglected to remove the "return;". You're right that it serves no purpose.
David Gelhar
But it is on many examples from Mac Dev Center. Look: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-BEHDEDDBGo to "Acessor Methods" session
okami
Yeah, I see that. But even there they are not consistent - some examples have it, but some don't. It definitely accomplishes nothing. Maybe some authors feel it adds clarity to explicitly say "return nothing"? (I don't happen to agree with that idea, but perhaps some do.)
David Gelhar