NSDate Memory Management Using ARC Over Release

I am building an application where an NSDate object is set using UIDatePicker from a standard View Controller. The date is added to a mutable dictionary in another class. When that same class tries to access the date object it has all ready been deallocated resulting in a crash. Additionally, this has only been a problem since compiling with Xcode 5.

Example Code

@interface SomeViewController ()

@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) ChangeTracker *changeTracker; // Records local changes made to managed objects to send to server

@end

@implementation SomeViewController

// Relevant implementation
- (void)touchSetPicker:(UIButton *)sender
{
    [self setDate:self.datePicker.date forManagedObject:self.someManagedObject];
}

- (void)setDate:(NSDate *)date forManagedObject:(NSManagedObject *)managedObject
{
    // Set properties on managed object based on date param

    // Pass date to changeTracker class
    [self.changeTracker setDate:date forManagedObject:managedObject];
}
@end

ChangeTracker.m

@interface ChangeTracker ()

@property (nonatomic, strong) NSMutableDictionary *dateChanges;

@end

@implementation ChangeTracker

- (void)setDate:(NSDate *)date forManagedObject:(NSManagedObject *)managedObject
{
    NSString *idProperty = managedObject.idProperty;

    self.dateChanges[idProperty] = date;
}

- (void)compileAllChanges
{
    for (NSString *idProperty in [self.dateChanges allKeys]) {

        // Here is where the crash occurs due to the date being deallocated
        NSDate *date = self.dateChanges[idProperty];
    }
}

@end

With zombie objects enabled I receive the error message: -[__NSDate release]: message sent to deallocated instance

The project is using ARC so I can't explicitly try to retain this object. Any help would be appreciated.

Answers


I am having the same problem described by @gabriel-ortega I am wondering if he had found any solution of what the cause was.

EDIT: To answer myself and hopefully help someone else too, I have found the cause of the problem.

As Apple documentation says:

To allow interoperation with manual retain-release code, ARC imposes a constraint on method naming: You cannot give an accessor a name that begins with new. This in turn means that you can’t, for example, declare a property whose name begins with new unless you specify a different getter:

// Won't work:
property NSString *newTitle;

// Works:
property (getter=theNewTitle) NSString *newTitle;

my property name was newPath so that explains...


You should not get zombies if you're using ARC. Do you have a variable declared as unsafe_unretained instead of weak? Or are you using a third party library somewhere? A library that uses manual referenced counted code internally could be a source of zombies.

Improper use of unsafe_unretained can cause you to reference an object that's been released, but with normal strong and weak references, weak references get zeroed out as soon as they are released, so you should not be able to send them messages once they've been released.

Another possibility is misuse of bridging casts. That can screw up ARC. Are you using bridging casts?


Need Your Help

Need alternative POST method for window.open, showmodeldialog and startDownload

javascript

In Javascript currently these functions are sending GET request window.open, showmodeldialog and startDownload.

Writing files in hdfs in C++ (libhdfs)

java c++ hadoop hdfs

I am trying to write an interface in C++ to write files in hdfs using libhdfs.so. My target hadoop version is 2.0.2-alpha