2

I might've jumped into Objective-C a little too fast and assumed I knew enough about memory management to dive in. Turns out I wasn't.

Fast forward a bit and I've caught up ... for the most part. One of my last remaining questions regards the @property/@synthesize process and setting those values properly.

I've often seen this:

SomeObject *obj = [[SomeObject alloc] init];
self.obj = obj;
[obj release];

It makes sense, but I wonder if this accomplishes a similar enough thing to be an alternative:

self.obj = [[[SomeObject alloc] init] autorelease];

Would that be acceptable anywhere you might set the value of the @property, obj? Or is direct access to the instance variable preferred over both of those in the init method?

obj = [[SomeObject alloc] init];

Thanks.

EDIT: Definitely related question that discusses half of my question. The autorelease part of my question is referenced in one of the answers, but has not been confirmed.

So my questions still remain:

  1. Is autorelease a valid alternative in this situation?
  2. Can autorelease be used this way in the init method, or should the instance variable always be set directly in the init method?
Community
  • 1
  • 1
joshhepworth
  • 2,976
  • 1
  • 16
  • 18
  • Closely related, possible duplicate: http://stackoverflow.com/questions/7842641/best-way-to-set-a-retained-property-to-a-newly-created-object – jscs Dec 13 '11 at 22:27

1 Answers1

3

The only difference is that in your first example, obj is released immediately (although it was presumably retained by the self.obj setter). The two examples are effectively the same.

Note that going forward, we can rely on ARC to deal with releasing objects at the appropriate time, so you can write:

self.obj = [[SomeObject alloc] init];

and let ARC worry about where to put the release or autorelease.

Update: You seem to be asking how things are different inside an -init method. The two things you need to be aware of are:

  1. When possible, you should access ivars directly in -init and -dealloc methods. The idea is to avoid problems that could occur if your accessors are overridden by a subclass. There are a number of other questions here on SO that address this in greater depth, such as:

    Why shouldn't I use Objective C 2.0 accessors in init/dealloc?

  2. If you're creating an object in an -init method and assigning it to an ivar, you probably don't want to release it until a later point, such as -dealloc. So yes, it's fine in an -init method to not release the objects you create (so long as you keep a reference to them), but you'll still balance that +alloc with a -release in -dealloc.

Community
  • 1
  • 1
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Ok, that makes sense then. So what happens in the `init` method or when the instance variable would have been `nil`? Should it be set directly, or is using the setter with `autorelease` fine? – joshhepworth Dec 13 '11 at 22:13
  • What do you mean by 'what happens in the init method?'? `-init` doesn't care about your variables. `+alloc` returns a pointer to an object. Then the `-init` message is sent to that object, and it returns a (probably the same, but possibly different) pointer. You can assign that to a local variable, as in your first example. Or you can assign it directly to a property, as in your second example. Or you can assign it directly to an instance variable if you don't mind managing the memory yourself. The setter with `autorelease` is fine. – Caleb Dec 13 '11 at 22:20
  • Sorry I wasn't clear. I meant in the `init` method of a class I would be writing. I was reading that the 'rules' can be different here because the instance variables are `nil` so calling `obj = [[SomeObject alloc] init];` would be correct instead of releasing in any way. Was that just a bunch of bologna? – joshhepworth Dec 14 '11 at 04:13
  • I think I understand what you're asking. I'll update my answer above rather than trying to squeeze it all into a comment. – Caleb Dec 14 '11 at 04:28