1

When i implement dealloc method i used to write:

[preopertyX release];

Today i found this code:

[self.propertyX release];

I'm not sure that this method is completely correct. What do you think about ? (we can take as assumption that propertyX is a retained and synthesized property).

MatterGoal
  • 16,038
  • 19
  • 109
  • 186

2 Answers2

2

From my understanding, you should either set it to nil or you can release the instance variable.

[self setPropertyX:nil]

or

[propertyX release]

The method you list can have unwanted side effects, but if you want the side actions of your setter to happen, I suggest setting to nil.

The accepted answer here: iPhone - dealloc - Release vs. nil is pretty good.

This is a nice debate outlining, pretty vibrantly, the methods available to you.

http://iphonedevelopment.blogspot.com/2010/09/dealloc.html followed up by http://www.red-sweater.com/blog/1423/dont-coddle-your-code

Community
  • 1
  • 1
griotspeak
  • 13,022
  • 13
  • 43
  • 54
  • I think you are talking about assigning nil. (using `=`) Set to nil should be very much like releasing, but with whatever side effects you have set up in the setter – griotspeak Apr 12 '11 at 12:14
  • Maybe i'm in wrong but, set to nil is similar to release if property is synthesized, because the first steps of an auto synthesized property are : **[property release]; property = new_value (this case = nil);** – MatterGoal Apr 12 '11 at 12:39
  • The multithreading argument in the blog post is bogus. Firstly, the setting to nil bit doesn't protect against the bug and secondly, why is the object being released if other threads are holding references to it? – JeremyP Apr 12 '11 at 12:53
  • @JeremyP - that's why the second blog post is there! – griotspeak Apr 12 '11 at 13:30
  • @griotspeak: Yes, I didn't read it until after I had posted my comment :) – JeremyP Apr 12 '11 at 13:32
1

You can write [self.propertyX release] if you have set propertyX retained and synthesized otherwise not

dks1725
  • 1,631
  • 1
  • 20
  • 29
  • In your opinion it behave like whether i write [propertyX release] ? – MatterGoal Apr 12 '11 at 12:07
  • You must also release if the property is (copy). I'd avoid using self.propertyX, because there's a chance (a slim chance, but still...) that a subclass might implement a getter method that has side effects that would be inappropriate in -dealloc. – Sherm Pendley Apr 12 '11 at 12:40
  • 1
    @Sherm Pendley: Also `self.property = nil;` will trigger KVO on the obbject being deallocated. – JeremyP Apr 12 '11 at 13:34