Do not use access methods in initializer and dealloc methods

I read about "Memory Management" and they do not recommend using access methods in the initialization method.

Question : why we should not use access methods in initialization method

I was looking for coupe links about this issue on stackoverflow and other sites. Nevertheless, I am not embarrassed yet.

Can anyone advise me on this, or if you can throw an example or a good link at me so that I can go through it myself. Thanks

+7
source share
4 answers

Here is an example I wrote that demonstrates two things:

  • how can I change the initialization order
  • how can leaks be introduced

Property initialization, dot notation

Although this example focuses on initialization, dealloc susceptible to similar categories of problems. As one concrete example: an object can partially resurrect itself in dealloc , and an imbalance in the reference counter becomes a potential danger.

In short, you want to focus on properly initializing and clearing the data your objects need, and not on behavioral problems / the impact of your objects on any subclasses.


Additional Information:

Why is myInstance = nil instead of self.myInstance = nil?

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

Best way to set a saved property for a newly created object

+2
source

This is a slightly religious issue with developers on both sides, and the advent of ARC has further confused the issue.

A few reasons:

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

  • The accessor may have side effects and again the object is not fully initialized. One common side effect is the creation of iVar upon first use.

These arguments can also apply to the use of accessories in dealloc (for code other than ARC).

+1
source

KVC supervisors monitor the methods of receipt and installation. If you are absolutely not sure that no one will ever watch your property, you ask for troubles. You have a defect where an observer twitches with a partially delloid object, it is very difficult to reproduce and it is almost impossible to verify.

+1
source

The main reason for not using access methods in intiailiser and dealloc methods is probably related to the development of Mac OS X and is probably not a problem for iOS. The development of the Mac OS X GUI includes a useful thing called β€œbindings”, which allows you to bind a control property to an object property, so when the user updates the control, the β€œbinding” will automatically update the property, and if the program updates the property (via access methods), "binding" will automatically update the control.

A lot of required materials are carried out taking into account key values ​​(I think). Observation of key values ​​is when an object observes changes in properties on other objects. Whenever you use the accessor method to change a property, any object that watches your object will be notified so that it can take action on the new property value. Using access methods can trigger any notification about monitoring key values ​​if you do not want them to occur, for example, during initialization and release, since any observers of your object will only deal with a partially initialized or partially released instance, and not completely initialized instance.

There is another major problem that occurs when your object is a subclass. When you use access methods to set the properties of your object, you actually call the access methods of your subclass (if it implements various access methods). This is unlikely to be a problem because inheritance in Objective-C is rare compared to .NET.

0
source

All Articles