Dancer
Dancer

Reputation: 17651

NSError release: message sent to deallocated instance

I am getting a crash on a iOS 7 app with the following error:

-[NSError release]: message sent to deallocated instance 0x3c443fe0

The error is initiated when I add a call to the following method:

-(void)loadMessages:(NSString*)customerUID {
  NSString *formatUID = [NSString stringWithFormat:@"%s%@%s", "'", customerUID, "'"];
  formatUID = [formatUID stringByReplacingOccurrencesOfString:@"'" withString:@"%27"];
  NSString *servicePath = [NSString stringWithFormat:@"/api/messagerecipient?messageid=null&customeruid=%@", formatUID];

  [[RKObjectManager sharedManager] getObjectsAtPath:servicePath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *messagesResult)
  {
    NSArray *messageResults = messagesResult.array;

    if (messageResults != nil || [messageResults count] != 0)
    {
      //Add some code here
    }
  } failure:^(RKObjectRequestOperation *operation, NSError *error) {

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
  }];
}

I added multiple breakpoints into the code at various points, and it's not returning any error details. Also, nothing in the console log indicates what the problem is (I have added full RestKit logging), just the above NSError release message.

I have also run a Zombie scan in Instruments. It shows the following.

Instruments

I'm confused because this shows that the zombie is being created by a GSEventRunModal call. When I go to Extended Detail and select the call, it shows the following:

Extended Detail

Any pointers would be gratefully appreciated, thanks.

Update: Instrument Extended Details stack trace

enter image description here

Upvotes: 4

Views: 1849

Answers (5)

Jake Hargus
Jake Hargus

Reputation: 362

In my case threading the database to a separate context helped. I used the following constructor on the class that was receiving the message:

    -(id)init {
    self = [super init];
    if (self) {
        self.managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        self.managedObjectContext.parentContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
        self.managedObjectContext.retainsRegisteredObjects = YES;
    }

    return self;
}

Upvotes: 0

Mike Gottlieb
Mike Gottlieb

Reputation: 966

I've seen this a lot as well and the root of the problem appears to be in Core Data. I use the MagicalRecord database library (so does RestKit) and we thought the error was there. You can see a discussion here. After all of our investigation it seemed like MagicalRecord was right and Core Data was at fault.

This had actually been filed as a bug that Apple claimed to have fixed, but we are still seeing it. The only way I've been able to work around this is by preventing every instance where I might not be able to save data so no error is reported. You can see some of those tips in the discussion thread linked to above.

Upvotes: 2

Ahmad Al-Attal
Ahmad Al-Attal

Reputation: 435

i think your problem is not with the method it self.

the error message says that you are sending a release call to an object of the type NSERROR.

please check the instance of the class which contains the method you are calling and make sure it's not deallocated.

or add the calling method to the question in order for us to check it.

Upvotes: 0

E. Rivera
E. Rivera

Reputation: 10938

Can you try to replace:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];

With:

NSString * message = [error localizedDescription];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];

Although I guess that the init method is getting a string anyway.

Upvotes: 0

Flexicoder
Flexicoder

Reputation: 8501

Could it be that you are trying to display an AlertView from inside a block? Interaction with the UI has to be on the main thread?

How do display a UIAlertView from a block on iOS?

Upvotes: 0

Related Questions