Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don’t Use Accessor Methods in Initializer Methods and dealloc [closed]

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

Question: why should not we use accessor methods in initializer method?

I am confusing about it.

like image 324
tranvutuan Avatar asked May 08 '12 19:05

tranvutuan


1 Answers

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:

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

like image 119
justin Avatar answered Sep 21 '22 01:09

justin