'How to resolve EXC_BAD_ADDRESS KERN_INVALID_ADDRESS when trying to deleteObject from NSManagedContext? (Objective-C iOS)

I was presented with the following issue in the production app but not in debug application, thus was not able to replicate it in the debug application.

I have read up on the following issue and understand that it meant something has been done to cause the pointer to be dereferenced and that memory location isn't inside on one of the chunks assigned to my program. The pointer used to point to memory was ok, but its chunk was deallocated. (Do correct my understanding, if this is wrong)

I have tried various ways like enabling "Guard Malloc" in xcode or putting in some environment variables

  1. NSAutoreleaseFreedObjectCheckEnabled
  2. NSZombieEnabled
  3. NSDebugEnabled

to further understand the error but to no avail.

The following code snippet is where the error has been thrown:

RSSNewsStorage.m

- (void) deleteAllNewsFeedsOffline:(NSManagedObjectContext *) moc
{
    NSError *error;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"SomeEntityName" inManagedObjectContext:moc];
    [fetchRequest setEntity:entityDescription];
    
    NSArray *items = [moc executeFetchRequest:fetchRequest error:&error];
    
    NSLog(@"PRINTING OUT ITEMS %@", items);
    
    for (RSSNewsEntry *managedObject in items) {
        [self deleteNewsImage:managedObject.enclosure];
        NSLog(@"PRINTING OUT managedObject %@", managedObject);
        [moc deleteObject:managedObject];
    }                                                          <--(This is line 41)
}

FetchFeeds

- (void)parseRss:(DDXMLElement *)rootElement entries:(NSMutableArray *)entries {
    dispatch_async([CoredataManager getBackgroundQueue], ^{
        NSManagedObjectContext *moc = [[CoredataManager sharedInstance] workerContext];
        [moc performBlock:^{
            NSArray *channels = [rootElement elementsForName:@"channel"];
            // Insert the data into DB
            NSManagedObjectContext *moc = [Helper mainManagedObjectContext];
            NSEntityDescription *newsEntity = [NSEntityDescription entityForName:@"SomeEntityName" inManagedObjectContext:moc];
            if (channels.count > 0) {
                [[RSSNewsStorage sharedInstance] deleteAllNewsFeedsOffline:moc];  <--(This is line 140)
            }

The following is the crashlytics error log:

Crashed: NSManagedObjectContext 0x28207e080
0  CoreFoundation                 0x20a78 __CFBasicHashAddValue + 252
1  CoreFoundation                 0x145d0 CFBasicHashAddValue + 2308
2  CoreData                       0x12262c -[NSManagedObjectContext deleteObject:] + 192
3  Share                          0x60dc8 -[RSSNewsStorage deleteAllNewsFeedsOffline:] + 41 (RSSNewsStorage.m:41)
4  Share                          0x70824 __31-[FetchFeeds parseRss:entries:]_block_invoke_2 + 140 (FetchFeeds.m:140)
5  CoreData                       0x1f834 developerSubmittedBlockToNSManagedObjectContextPerform + 156
6  libdispatch.dylib              0x3a2c _dispatch_client_callout + 20
7  libdispatch.dylib              0xb124 _dispatch_lane_serial_drain + 668
8  libdispatch.dylib              0xbc80 _dispatch_lane_invoke + 392
9  libdispatch.dylib              0x16500 _dispatch_workloop_worker_thread + 648
10 libsystem_pthread.dylib        0x10bc _pthread_wqthread + 288
11 libsystem_pthread.dylib        0xe5c start_wqthread + 8


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source