NOTE: This was due to a bug in some XCode beta versions that has long been fixed. This Question and answer will probably not help you if you have problems with ARC.


I am migrating my project from manual reference counting to ARC, and have stumbled upon a problem: How can I ensure that a custom setter for a retain property actually retains?

In myClass.h, I've declared a property: @property (retain) NSDate *date. It doesn't matter whether I manually set a __strong ivar or have it autogenerated.

In the implementation, I have of course @synthesize date, and implemented a custom setter (or just download the demo Xcode project):

- (void)setDate:(NSDate *)newDate
{
  if (allowedToSetNewDate)
  {
    date = newDate;
  }
}

This does not seem to retain the date, and gives me message sent to deallocated instance when newName is (auto-) released where it came from, when trying to access myClass.date later (provided Zombie is enabled; otherwise, it just crashes silently).

Changing the setter to use date = [newDate copy] works around the error, but isn't really what I want. Deleting the custom setter also works, but is obviously not desireable.

What am I missing here? How can I ensure that a custom setter for a retain property actually retains in an ARC environment? This seems such a basic and common task that I think I'm overlooking something very obvious.

(NOTE: This does not fall under the terms of any Apple NDA, as ARC is publically released as part of LLVM)

EDIT: I've created a small Xcode project to demo the issue and uploaded it to github. Feel free to download it and play around. I'm at my

Edit
Report