Take the 2-minute tour ×

I am doing some reading about the Memory Management and they not recommend to use accessor methods in initializer method.

Question : why should not we use accessor methods in initilizer method

I was searching coupe of references about this issue on stackoverflow and other sites as well. However, I am not still confusing about it.

Can anybody advice me on this issue or if you can throw at me an example or a good reference so that I can go thru it by myself. Thanks

share|improve this question
Which material have you already read on the subject? Which bits confuse you? –  Jim May 8 '12 at 19:42
Have you took a look at the link I embedded. Actually, my question is also the subject of a thread. I did an edit so you can see my questin now. –  ttran May 8 '12 at 19:56
add comment

closed as not constructive by casperOne May 10 '12 at 21:21


4 Answers

up vote 1 down vote accepted

Here's an example I wrote which demonstrates two things:

  • how initialization can be reordered
  • how leaks can be introduced

Initializing a property, dot notation

Although the example focuses on initialization, dealloc is susceptible to similar categories of problems. As one specific example: an object may partially resurrect itself in dealloc, and reference count imbalances become a potential danger.

Briefly, you want to focus on correct initialization and cleanup of the data your objects need -- rather than the behavioral concerns/influence of your objects through any subclasses.

More reading:

(iphone) why myInstance = nil instead of self.myInstance = nil?

Should I refer to self.property in the init method with ARC?

Best way to set a retained property to a newly created object

share|improve this answer
add comment

That is a bit of a religious issue with developers on both sides and the arrival of ARC has further muddied the issue.

A couple of reasons are:

  1. The object is not fully initialized and the accessor may depend on a fully initialized object.

  2. The accessor may have side effects and again the object is not fully initialized. One common side effect is instantiating an iVar on first use.

These arguments can also apply to using accessors in dealloc (for non-ARC code).

share|improve this answer
add comment

KVC observers monitor the getter and setter methods. Unless you are absolutely certain that no-one will ever observe your property, then you are asking for trouble. Have a defect where an observer is mucking around with a partially dealloc'ed object is very hard to reproduce and nearly impossible to test for.

share|improve this answer
add comment

The main reason behind not using accessor methods in intiailiser methods and dealloc probably stems from Mac OS X development and is probably not so much of an issue on iOS. Mac OS X GUI development involves a useful thing called "bindings" which allows you to bind a control's property to a property of an object, so that when the user updates the control, the "binding" will automatically update the property, and if the program updates the property (via the accessor methods), the "binding" will automatically update the control.

A lot of binding stuff is done with Key-Value Observing (I think). Key-Value Observing is when an object observes changes to properties on other objects. Whenever you use an accessor method to change a property, any object that is observing your object will be notified so that it can take action on the new value of the property. Using accessor methods can trigger any Key-Value Observing notifications when you don't want them to occur, such as during initialisation and deallocation, because any observers of your object will be dealing with only a partially initialised or partially deallocated instance rather than a fully initialised instance.

There is also another main area of concern, and that is when your object is subclassed. When you use the accessor methods to set properties of your object, you will actually be invoking the accessor methods of your subclass (if it implements different accessor methods). This is less likely to be an issue because inheritance in Objective-C development is rare compared to say .NET.

share|improve this answer
The main reason behind not using accessor methods in intiailiser methods and dealloc probably stems from Mac OS X development and is probably not so much of an issue on iOS… no. bindings add to the list of potential side effects. side effects (read: bugs) can exist without using bindings, observers, ui elements, and so on. here's the demo i wrote, which uses foundation types: stackoverflow.com/questions/5932677/… –  justin May 8 '12 at 20:53
@Justin: I mentioned subclassing at the end of my answer. –  dreamlax May 8 '12 at 21:06
yes. i saw that before commenting. –  justin May 8 '12 at 21:22
add comment
