antalkerekes
antalkerekes

Reputation: 2128

Message sent to deallocated instance, when working with an NSManagedObject indirectly

I have this code that is giving me a headache:

/ original code removed, see current version below /

The message I receive:

*** -[__NSDate class]: message sent to deallocated instance 0x69fa350

EDIT 3:

To test it, I have moved everything into one method, and verified that it still produces the same error. This is a splitView iPad app. A voiceMemo object is passed by the left-side table view controller upon selection. The right side view has a tableView on its own, this is where I am loading the reminders.

What happens, is this: Initially, everything loads correctly. Without any reminder data in my database, I can select any entry, it loads correctly. I can delete them, modify them, etc. I can select an entry with reminder data, and it loads correctly. The table view in the detail view is correctly populated with data. However, trying to modify a reminder, the program crashes: message sent to deallocated instance. Without trying to modify any data, the program crashes if I try to load another entry WITH reminder data. I can delete entries which have no reminder from the (left side) main table view, but when trying to delete one with reminder data, it crashes (on the save method). The next time I start the program, the entry is deleted, though.

The database and the custom classes are shared with the iPhone version, which works correctly.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* only relevant parts posted */

    // Left-side label (title)

    NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc]     
        initWithKey:@"DBreminderDate" ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSMutableArray *sortedReminders = [NSMutableArray arrayWithArray:
        [voiceMemo.reminders allObjects]];

    [sortedReminders sortUsingDescriptors:sortDescriptors];
    self.remindersArray = sortedReminders;    

    AKReminder *reminder = [remindersArray objectAtIndex:indexPath.row];

    cell.textLabel.text = reminder.DBreminderTitle;

    if ([cell.textLabel.text length] == 0) {
        cell.textLabel.text = @"Reminder";
    }

    // Right-side label (date)

    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [format setLocale:locale];
    [locale release];
    NSDate *reminderSetTo = reminder.DBreminderDate;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [format     
        stringFromDate:reminderSetTo]]; // breaks here

    return cell;
}

My data model:

Core Data data model

Upvotes: 1

Views: 457

Answers (1)

sergio
sergio

Reputation: 69047

This statement:

    NSArray *sortDescriptors = [[NSArray alloc] 
    initWithObjects:&sortDescriptor count:1];

looks strange to me. Do you mean:

NSArray *sortDescriptors = [[NSArray alloc] 
    initWithObjects:sortDescriptor count:1];

?

Any object you add to an NSArray should derive from NSObject; &sortDescriptor does not (is the address in memory of a pointer to sortDescriptor). This could be causing an improper attempt to deallocate &sortDescriptor instead of sortDescriptor when sortDescriptors is deallocated...

Upvotes: 1

Related Questions